Plugin Cafe Homepage
Forum Home Forum Home > Plugin Cafe > PYTHON Development
  New Posts New Posts
  FAQ FAQ  Forum Search

Boundingbox Size of Point selection

Author
Message
  Topic Search Topic Search
holgar View Drop Down
Member
Member


Joined: 2004 Jun 21
Location: Germany
Online Status: Offline
Posts: 85
Direct Link To This Post Topic: Boundingbox Size of Point selection
    Posted: 2015 Dec 21 at 2:34am
Hi, I am trying to get the Size of a Pointselection...basically what obj.GetRad() is for Objectsize...but for selected Points.

Is there an easy way?
Back to Top
NiklasR View Drop Down
Member
Member


Joined: 2010 Dec 13
Location: Germany
Online Status: Offline
Posts: 2575
Direct Link To This Post Posted: 2015 Dec 21 at 3:28am
The easy way is to calculate it. :) It's simple for a given set of points. Just make sure you use a copy of
the starting point, otherwise you'll have two times the same vector and that will lead to wrong results.

minv = c4d.Vector(points[0])
maxv = c4d.Vector(points[0])
Back to Top
holgar View Drop Down
Member
Member


Joined: 2004 Jun 21
Location: Germany
Online Status: Offline
Posts: 85
Direct Link To This Post Posted: 2015 Dec 21 at 3:35am
I tried this...whats not working here?


def GetSelectionSize(op):


    sel = op.GetPointS()
    points = [(i, point)
          for i, point in enumerate(op.GetAllPoints())
          if sel.IsSelected(i)]

    if not points:
      return

    selsizemin = min(c4d.Vector(point[1]) for point in points)
    selsizemax = max(c4d.Vector(point[1]) for point in points)
    selsize= selsizemax-selsizemin
    return selsize


Back to Top
NiklasR View Drop Down
Member
Member


Joined: 2010 Dec 13
Location: Germany
Online Status: Offline
Posts: 2575
Direct Link To This Post Posted: 2015 Dec 21 at 4:10am
You have to find the min/max for each component (x, y, z) of all points, not the min/max of all points.
Back to Top
NiklasR View Drop Down
Member
Member


Joined: 2010 Dec 13
Location: Germany
Online Status: Offline
Posts: 2575
Direct Link To This Post Posted: 2015 Dec 21 at 4:11am
Also, why do you write point[1] ? That'll give you the Y component of the Vector.
Back to Top
holgar View Drop Down
Member
Member


Joined: 2004 Jun 21
Location: Germany
Online Status: Offline
Posts: 85
Direct Link To This Post Posted: 2015 Dec 21 at 4:11am
I guess you are rightLOL
Back to Top
holgar View Drop Down
Member
Member


Joined: 2004 Jun 21
Location: Germany
Online Status: Offline
Posts: 85
Direct Link To This Post Posted: 2015 Dec 21 at 4:56am
This Works now. I guess its more complicated than it could be ;-)

def GetSelectionSize(op):


    sel = op.GetPointS()
    points = [(i, point)
          for i, point in enumerate(op.GetAllPoints())
          if sel.IsSelected(i)]

    if not points:
      return

    selsizeminX = min(point[1].x for point in points)
    selsizemaxX = max(point[1].x for point in points)
    selsizeminY = min(point[1].y for point in points)
    selsizemaxY = max(point[1].y for point in points)
    selsizeminZ = min(point[1].z for point in points)
    selsizemaxZ = max(point[1].z for point in points)

    boundbox= c4d.Vector(selsizemaxX,selsizemaxY,selsizemaxZ)-c4d.Vector(selsizeminX,selsizeminY,selsizeminZ)
    selsize=boundbox.GetLength()
    return selsize

Back to Top
rown View Drop Down
Member
Member


Joined: 2012 Apr 24
Online Status: Offline
Posts: 100
Direct Link To This Post Posted: 2015 Dec 21 at 5:00am
Just a question, because Im instrested in: Wouldnt it be better to do all in one for-loop?
Back to Top
rown View Drop Down
Member
Member


Joined: 2012 Apr 24
Online Status: Offline
Posts: 100
Direct Link To This Post Posted: 2015 Dec 21 at 6:05am
I tried it out. Im not sure if its the best practice, but the results.

import c4d
from datetime import datetime
#Welcome to the world of Python

class stopwatch():
   
    def __init__(self):
        start = datetime.now()
       
    def start(self):
        self.start = datetime.now()
        return self.start
       
    def stop(self):
        return datetime.now()-self.start
   
def GetSelectionSize(op):

    sel = op.GetPointS()
    points = [(i, point)for i, point in enumerate(op.GetAllPoints())if sel.IsSelected(i)]

    if not points: return

    selsizeminX = min(point[1].x for point in points)
    selsizemaxX = max(point[1].x for point in points)
    selsizeminY = min(point[1].y for point in points)
    selsizemaxY = max(point[1].y for point in points)
    selsizeminZ = min(point[1].z for point in points)
    selsizemaxZ = max(point[1].z for point in points)

    boundbox = c4d.Vector(selsizemaxX,selsizemaxY,selsizemaxZ)-c4d.Vector(selsizeminX,selsizeminY,selsizeminZ)
    selsize = boundbox.GetLength()
    return selsize

def GetSelectionSize2(obj):
    v_min = None
    v_max = None
    for i, sel in enumerate(obj.GetPointS().GetAll(obj.GetPointCount())):
        if sel:
            p = obj.GetPoint(i)
            if v_min == None: v_min = c4d.Vector(p.x, p.y, p.z)
            if v_max == None: v_max = c4d.Vector(p.x, p.y, p.z)
           
            v_min.x = min(v_min.x, p.x)
            v_min.y = min(v_min.y, p.y)
            v_min.z = min(v_min.z, p.z)
            v_max.x = max(v_max.x, p.x)
            v_max.y = max(v_max.y, p.y)
            v_max.z = max(v_max.z, p.z)
           
    boundbox = v_max - v_min
    selsize = boundbox.GetLength()
    return selsize
   
   
def main():
    obj = op.GetDown()
   
    t1 = stopwatch()
    t1.start()
    size = GetSelectionSize(obj)
    print t1.stop()
    print size
   
   
    t1 = stopwatch()
    t1.start()
    size = GetSelectionSize2(obj)
   
    print t1.stop()
    print size
  
    return None


Pointcount 600.001 and 300.000 selected:
GetSelectionSize(obj): 0.022sec.
GetSelectionSize2(obj): 0.017sec.

Pointcount 1.500.001 and 750.000 selected:
GetSelectionSize(obj): 0.469sec.
GetSelectionSize2(obj): 0.137sec.


Back to Top

Forum Jump Forum Permissions View Drop Down

Bulletin Board Software by Web Wiz Forums® version 9.61 [Free Express Edition]
Copyright ©2001-2009 Web Wiz

This page was generated in 0.094 seconds.