Check to see if a point is inside bounding box

Check to see if a point is inside bounding box

Anonymous
Not applicable
7,313 Views
9 Replies
Message 1 of 10

Check to see if a point is inside bounding box

Anonymous
Not applicable

I'm working through a 4 year old example that uses code that's no longer valid. 

 

    // test if the bounding box contains our point:

    if( Geometry.BoundingBoxXYZContains(
      newRoom.get_BoundingBox( null ), 
      xyz ) )
    {      
      break;  
    }

 

I cant find any reference to 'Geometry.BoundryBoxXYZContains'. How would this be acomplished? Thanks. 

0 Likes
7,314 Views
9 Replies
Replies (9)
Message 2 of 10

Anonymous
Not applicable

Not sure if there is a pre-rolled API method any more but you could just do it manually by comparing the test point to the points gathered by Min & Max properties of the BoundingBoxXYZ object gathered from your room.

0 Likes
Message 3 of 10

Anonymous
Not applicable

Yes, that worked. I'm still having trouble getting the whole addin to work though. I'm creating unplaced rooms in from an excel file and populating a listbox with the unplaced rooms. I'm trying to place thoes rooms. I'm getting transaction errors, room height errors, you name it. 

 

foreach (PlanCircuit circuit in planTopology.Circuits)
                {
                    if (!circuit.IsRoomLocated)
                    {                      
                        Room selectedRoom = (Room)box.SelectedItem;

                        Room newRoom = doc.Create.NewRoom(selectedRoom, circuit);
                        BoundingBoxXYZ BB = newRoom.get_BoundingBox(null);

                        if (BB.Min.X < selectedPoint.X && BB.Min.Y < selectedPoint.Y)
                        {
                            if (BB.Max.X > selectedPoint.X && BB.Max.Y > selectedPoint.Y)
                            {
                                trans.Commit();                                
                                break;
                            }
                        }
                        else
                        {
                            doc.Delete(newRoom);
                            newRoom = null;
                        }
                    }
                }

 

Thanks.

0 Likes
Message 4 of 10

jeremytammik
Autodesk
Autodesk

Dear Bryan,

Geometry.BoundingBoxXYZContains is not a standard Revit API method, as far as I know, and never was.

Probably it was implemented by someone as a custom extension method along the lines suggested by scottwilson3525.

Here is a sample implementation of a suitable comparison operator for the XYZ point class:

  const double _eps = 1.0e-9;

  public static bool IsZero(
    double a,
    double tolerance )
  {
    return tolerance > Math.Abs( a );
  }

  public static bool IsZero( double a )
  {
    return IsZero( a, _eps );
  }

  public static bool IsEqual( double a, double b )
  {
    return IsZero( b - a );
  }

  public static int Compare( double a, double b )
  {
    return IsEqual( a, b ) ? 0 : ( a < b ? -1 : 1 );
  }

  public static int Compare( XYZ p, XYZ q )
  {
    int d = Compare( p.X, q.X );

    if( 0 == d )
    {
      d = Compare( p.Y, q.Y );

      if( 0 == d )
      {
        d = Compare( p.Z, q.Z );
      }
    }
    return d;
  }

  public static bool IsEqual( XYZ p, XYZ q )
  {
    return 0 == Compare( p, q );
  }

Now you can implement BoundingBoxXyzContains like this:

  /// <summary>
  /// Return true if the given bounding box bb
  /// contains the given point p in its interior.
  /// </summary>
  public bool BoundingBoxXyzContains(
    BoundingBoxXYZ bb,
    XYZ p )
  {
    return 0 < Compare( bb.Min, p )
      && 0 < Compare( p, bb.Max );
  }

 

I'm unable to say anything about all the room placement errors you are getting, though. Sorry.

 

That's out of the scope of this thread, anyway  🙂

 

I hope this helps anyway.

Best regards,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 5 of 10

Anonymous
Not applicable

I'm glad you got some of it sorted out.

 

In regard to the transaction errors, I see that you are commiting your transaction within the foreach loop when a match is found and then breaking out of the loop. What are you doing after this? If you are modifying the database again after this code then you will get transaction errors. To fix this, just move the comit further down in your code (at end of method maybe?). Also check that your are not attempting to create a new transaction within an already active one.

0 Likes
Message 6 of 10

joshua.lumley
Advocate
Advocate

Hi Jeremry, correct me if I'm wrong, but should the Compare parameters be the other way round implementation and should actually be like this:

        public bool BoundingBoxXyzContains(BoundingBoxXYZ bb,XYZ p)
        {
            return 0 < Compare(p, bb.Min)
              && 0 < Compare(bb.Max, p);
        }
0 Likes
Message 7 of 10

sonicer
Collaborator
Collaborator
0 Likes
Message 8 of 10

jeremytammik
Autodesk
Autodesk

I would have expected the original to be correct. I don't remember how it was tested. Normally, in .NET, Compare(a,b) returns a negative number if a is smaller than b, zero if equal, and positive otherwise. E.g.., for strings:

 

https://docs.microsoft.com/en-us/dotnet/api/system.string.compare?view=netframework-4.8

 

All overloads of the Compare method return a 32-bit signed integer indicating the lexical relationship between the two comparands:

 

  • Value -- Condition
  • Less than zero The first substring precedes the second substring in the sort order.
  • Zero The substrings occur in the same position in the sort order, or length is zero.
  • Greater than zero The first substring follows the second substring in the sort order.

 

Step through in the debugger and let us know, please. We absolutely must ensure that it works correctly, being sample code that people refer to...

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 9 of 10

joshua.lumley
Advocate
Advocate

The inverted code works on my current project but maybe everything is mirrored or otherwise quirky that makes it different. I will be sure after the inverted code is tested on several different projects.

0 Likes
Message 10 of 10

jeremytammik
Autodesk
Autodesk

The Compare method that you are calling there might be set up wrong, returning an inverted result.

 

That is why I suggest stepping through all the nested calls in a debugger and ensuring that none of the nested steps is defined the wrong way round.

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes