Individually colouring points

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
j-dowsett
Posts: 210
Joined: Wed Sep 07, 2011 9:37 am

Individually colouring points

Post by j-dowsett »

Hi

In the points workbench, is it possible to colour each point in a collection individually (presumably via python)? I have in the region of 5000 points so I don't really want each point as a separate object in the tree.

This actually comes about from having a 3D sample space, with each sample point having an error associated with it - so I figured that having sample points in FreeCAD and colouring by the error might be a good way of visualising the data.


Cheers
Joe


doh!

OS: Ubuntu 14.04 LTS
Word size: 64-bit
Version: 0.14.3681 (Git)
Branch: master
Hash: ef08a61a4dbec598101e0c1aead95a031ef53f7b
Python version: 2.7.6
Qt version: 4.8.6
Coin version: 4.0.0a
SoQt version: 1.6.0a
OCC version: 6.7.0
wmayer
Founder
Posts: 20324
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Individually colouring points

Post by wmayer »

Code: Select all

import Points

class PointsFeature:
    def __init__(self, obj):
        obj.addProperty("App::PropertyColorList","Colors","Points","Ponts").Colors=[]
        obj.Proxy = self

    def execute(self, fp):
        pass

FreeCAD.newDocument()
a=FreeCAD.ActiveDocument.addObject("Points::FeaturePython","Points")
PointsFeature(a)
a.ViewObject.Proxy=0

p=Points.Points()
p.addPoints([App.Vector(0,0,0),App.Vector(1,1,1),App.Vector(2,2,2)])
a.Points=p

c=[(1.0,0.0,0.0),(0.0,1.0,0.0),(0.0,0.0,1.0)]
a.Colors=c
a.ViewObject.DisplayMode="Color"
j-dowsett
Posts: 210
Joined: Wed Sep 07, 2011 9:37 am

Re: Individually colouring points

Post by j-dowsett »

Ooooh! Cool, I'll try that out tomorrow but on the face of it it looks perfect! Thanks :)
j-dowsett
Posts: 210
Joined: Wed Sep 07, 2011 9:37 am

Re: Individually colouring points

Post by j-dowsett »

...so I couldn't wait till tomorrow!
samples_results.png
samples_results.png (41.98 KiB) Viewed 3101 times
Beautiful, thanks very much!
User avatar
shoogen
Veteran
Posts: 2823
Joined: Thu Dec 01, 2011 5:24 pm

Re: Individually colouring points

Post by shoogen »

I wanted this function for years :shock:

Code: Select all

import FreeCAD
if open.__module__ == '__builtin__':
    pythonopen = open # to distinguish python built-in open function from the one declared here

class PointsFeature:
    def __init__(self, obj):
        obj.addProperty("App::PropertyColorList","Colors","Points","Ponts").Colors=[]
        obj.Proxy = self

    def execute(self, fp):
        pass

def parsepts(filename):
    f1=pythonopen(filename)
    color=[]
    points=[]
    for line in f1:
        x,y,z,r,g,b=[float(s) for s in line.strip().split(' ')[0:6] ]
        points.append(FreeCAD.Vector(x,y,z))
        color.append((r/255.0,g/255.0,b/255.0))
    return points,color

def createcoloredpointdcloud(doc,points,color):
    import Points
    obj=doc.addObject("Points::FeaturePython","Points")
    PointsFeature(obj)
    obj.ViewObject.Proxy=0

    p=Points.Points()
    p.addPoints(points)
    obj.Points=p
    obj.Colors=color
    if FreeCAD.GuiUp:
        obj.ViewObject.DisplayMode="Color"
    return obj

def open(filename):
    import os.path
    docname = os.path.splitext(os.path.basename(filename))[0]
    doc = FreeCAD.newDocument(docname)
    points,color = parsepts(filename)
    createcoloredpointdcloud(doc,points,color)
    return doc

def insert(filename,docname):
    try:
        doc=FreeCAD.getDocument(docname)
    except:
        doc=FreeCAD.newDocument(docname)
    points,color = parsepts(filename)
    createcoloredpointdcloud(doc,points,color)

def registerFCTypes():
    import FreeCAD
    FreeCAD.addImportType("colored poind cloud (*.pts)","coloredpts")

try:
    import FreeCAD
    registerFCTypes()
except ImportError:
    pass
Wouldn't it make sense to implement the importer in c++ and include it in the points module?
User avatar
NormandC
Veteran
Posts: 18589
Joined: Sat Feb 06, 2010 9:52 pm
Location: Québec, Canada

Re: Individually colouring points

Post by NormandC »

Since this is about python scripting I'm moving this topic from the Help forum to the appropriate forum.
Post Reply