Bounding Box with C++

Ok, let us just forget about that for the moment and come to the actual problem…

This is the first task, that I should do for the homework:

What does it mean, that the corner points are given as pointers? Are given to what?! as pointers?! …

If I put that code found here: http://developer.rhino3d.com/samples/cpp/add-brep-box/
to generate a Brep Box, it tells me, that context is not defined …

1234.txt (1.7 KB)

Why is context not known here? … Thanks!

https://www.microsoft.com/en-us/download/details.aspx?id=23691

Its your code - you tell me. :wink:

Without seeing your code, I can only guess. But since “our corner points are given as pointers”, my guess is that “my_Point_000”, “my_Point_100”, etc. are declared as follows:

ON_3dPoint* my_Point_000 = new ON_3dPoint(0.0, 0.0, 0.0);
ON_3dPoint* my_Point_100 = new ON_3dPoint(1.0, 0.0, 0.0);
ON_3dPoint* my_Point_110 = new ON_3dPoint(1.0, 1.0, 0.0);
ON_3dPoint* my_Point_010 = new ON_3dPoint(0.0, 1.0, 0.0);
ON_3dPoint* my_Point_001 = new ON_3dPoint(0.0, 0.0, 1.0);
ON_3dPoint* my_Point_101 = new ON_3dPoint(1.0, 0.0, 1.0);
ON_3dPoint* my_Point_111 = new ON_3dPoint(1.0, 1.0, 1.0);
ON_3dPoint* my_Point_011 = new ON_3dPoint(0.0, 1.0, 1.0);

If this is the case, then you can do something like this:

ON_3dPointArray corners;
corners.Append(*my_Point_000);
corners.Append(*my_Point_100);
corners.Append(*my_Point_110);
corners.Append(*my_Point_010);
corners.Append(*my_Point_001);
corners.Append(*my_Point_101);
corners.Append(*my_Point_111);
corners.Append(*my_Point_011);

ON_Brep* pBrep = ON_BrepBox(corners);
if (0 != pBrep)
{
  CRhinoBrepObject* pObject = new CRhinoBrepObject();
  pObject->SetBrep(pBrep); // pObject now owns the memory pointed to by pBrep
  if (context.m_doc.AddObject(pObject))
    context.m_doc.Redraw();
  else
    delete pObject; // Don't leak...
}

When finished, you will want to delete any allocated memory so you don’t leak.

// Clean up memory so we don't leak...
delete my_Point_000;
delete my_Point_100;
delete my_Point_110;
delete my_Point_010;
delete my_Point_001;
delete my_Point_101;
delete my_Point_111;
delete my_Point_011;

Hope this helps.

1 Like

I don’t know, but here is what I “did to the code” until now:
Now, this is the TGFRhinoPlugin_BBox.cxx file …
Is this OK?


//--- Include Precompiled header
#include "StdAfx.h"

// ---- Internal Includes ---- 
#include "TGFRhinoPlugin_BBox.h"
#include "stdafx.h"
// ---- Other Includes ---- 

/***************************************************************************
 **** class CiEPlg_BBox : Constructor                                   ****
 ***************************************************************************/
CiEPlg_BBox::CiEPlg_BBox(BBoxType aBBoxType,double epsval)
{
    // init
    gl_BBoxType = aBBoxType;
    // nullify pointer to corner points    
    my_Point_000 = 0;
    my_Point_100 = 0;
    my_Point_110 = 0;
    my_Point_010 = 0;

    my_Point_001 = 0;
    my_Point_101 = 0;
    my_Point_111 = 0;
    my_Point_011 = 0;
    // center point init
    my_CenterPoint = 0;
    // eps value
    gl_epsval = epsval;
    // half length distances
    gl_rx = MFLOAT;
    gl_ry = MFLOAT;
    gl_rz = MFLOAT;
}


/***************************************************************************
 **** class CiEPlg_BBox : Destructor                                    ****
 ***************************************************************************/
/**
 * \fn ~CiEPlg_BBox()
 * \brief Destructor of the class CiEPlg_BBox
 */
CiEPlg_BBox::~CiEPlg_BBox()
{
    // delete dynamic elements
    delete my_Point_000;
    delete my_Point_100;
    delete my_Point_110;
    delete my_Point_010;

    delete my_Point_001;
    delete my_Point_101;
    delete my_Point_111;
    delete my_Point_011;

    delete my_CenterPoint;
}


/***************************************************************************
 **** class CiEPlg_BBox : member routine                                 ****
 ***************************************************************************/
BOOL CiEPlg_BBox::doVisualBBox()
{
    // check validity
    if(my_Point_000 == 0 || my_Point_100 == 0 || my_Point_110 == 0 || my_Point_010 == 0 ||
        my_Point_001 == 0 || my_Point_101 == 0 || my_Point_111 == 0 || my_Point_011 == 0) {
            return CRhinoCommand::failure;
    }

    CRhinoDoc* doc = RhinoApp().ActiveDoc();

    // ********* YOUR JOB ***

    // a ON_BrepBox shall be created and visualized in Rhino.
    // the box is defined by 8 corner points as you see in the lines above.
    // how to do that is shown also here: http://wiki.mcneel.com/developer/sdksamples/addbrepbox
    // Be aware that our corner points are given as pointers!

    // ********* YOUR JOB ***

    
  // define the corners of the box

    CRhinoCommand::result rc = CRhinoCommand::nothing;

    ON_3dPointArray boxcorners;
  boxcorners.Append(ON_3dPoint(my_Point_000->x, my_Point_000->y , my_Point_000->z  ));
  boxcorners.Append(ON_3dPoint(my_Point_100->x, my_Point_100->y , my_Point_100->z  ));
  boxcorners.Append( ON_3dPoint(my_Point_110->x, my_Point_110->y , my_Point_110->z  ) );
  boxcorners.Append( ON_3dPoint(my_Point_010->x, my_Point_010->y , my_Point_010->z  ) );
  boxcorners.Append( ON_3dPoint(my_Point_001->x, my_Point_001->y , my_Point_001->z  ) );
  boxcorners.Append( ON_3dPoint(my_Point_101->x, my_Point_101->y , my_Point_101->z  ) );
  boxcorners.Append(ON_3dPoint(my_Point_111->x, my_Point_111->y , my_Point_111->z  )  );
  boxcorners.Append(  ON_3dPoint(my_Point_011->x, my_Point_011->y , my_Point_011->z  ));

  // Build the brep  
  ON_Brep* pBrep = ON_BrepBox( boxcorners );
  if( pBrep )
  {
    CRhinoBrepObject* pObject = new CRhinoBrepObject();
    pObject->SetBrep( pBrep );
    if( doc->AddObject(pObject) )
    {
      doc->Redraw();
      rc = CRhinoCommand::success;
    }
    else
    {
      delete pObject;
      pObject = 0;
      rc = CRhinoCommand::failure;
    }
  }
  return rc;

}



  


/***************************************************************************
 **** class CiEPlg_BBox : member routine                                 ****
 ***************************************************************************/
bool CiEPlg_BBox::get_MinXYZ(double& X, double& Y, double& Z)
{
    // check validity
    if(my_Point_000 == 0 || my_Point_100 == 0 || my_Point_110 == 0 || my_Point_010 == 0 ||
        my_Point_001 == 0 || my_Point_101 == 0 || my_Point_111 == 0 || my_Point_011 == 0) {
            return false;
    }

    // return values
    X = my_Point_000->x;
    Y = my_Point_000->y;
    Z = my_Point_000->z;

    // return 
    return true;
}


/***************************************************************************
 **** class CiEPlg_BBox : member routine                                 ****
 ***************************************************************************/
bool CiEPlg_BBox::get_MaxXYZ(double& X, double& Y, double& Z)
{
    // check validity
    if(my_Point_000 == 0 || my_Point_100 == 0 || my_Point_110 == 0 || my_Point_010 == 0 ||
        my_Point_001 == 0 || my_Point_101 == 0 || my_Point_111 == 0 || my_Point_011 == 0) {
            return false;
    }

    // return values

    X = my_Point_111->x;
    Y = my_Point_111->y;
    Z = my_Point_111->z;

    // return 
    return true;
}

The CiEPlg_BBox is an abstract class. In the previous Code, you could see the C++ file for that class, which is TGFRhinoPlugin_BBox.cxx.

And here is the header file with the name: TGFRhinoPlugin_BBox.h

… I just have too many files …

 /**
 * \file TGFRhinoPlugin_BBox.h
 */
#ifndef CIEPLG_BBOX_HEADER
#define CIEPLG_BBOX_HEADER
/***************************************************************************
 ****                                                                   ****
 ****    Header:     TGFRhinoPlugin_BBox.h                              ****
 ****                                                                   ****
/***************************************************************************
 **** Declarations                                                      ****
 ***************************************************************************/
// ---- Internal Classes ----
BOOL;
/***************************************************************************
 **** Includes                                                          ****
 ***************************************************************************/
// ---- Internal Includes ----
#include "TGFRhinoPluginGlobal.h"
// ---- Rhino SDK Includes -----
#include "RhinoSdk.h"
// ---- Std Includes ----
#include <map>
/***************************************************************************
 **** class CiEPlg_BBox                                                 ****
 ***************************************************************************/
class CiEPlg_BBox 
{
public:
// ---- Public variables ----
    BBoxType                gl_BBoxType;
    // epsilon of bboxes
    double                    gl_epsval;
    // half length distances
    double                    gl_rx;
    double                    gl_ry;
    double                    gl_rz;
protected:
// ---- protected variables ----
    // bbox corner points in global coordinates    
    ON_3dPoint*                my_Point_000;
    ON_3dPoint*                my_Point_100;
    ON_3dPoint*                my_Point_110;
    ON_3dPoint*                my_Point_010;
    ON_3dPoint*                my_Point_001;
    ON_3dPoint*                my_Point_101;
    ON_3dPoint*                my_Point_111;
    ON_3dPoint*                my_Point_011;
    // center point
    ON_3dPoint*                my_CenterPoint;
private:
// ---- Private variables ----
public:
// ---- Constructors and Destructors ----
    CiEPlg_BBox(BBoxType aBBoxType,double epsval = EPS);
    ~CiEPlg_BBox();
// ---- Member access functions ----
    // get min point values
    ON_3dPoint*    get_Center() {return my_CenterPoint;}
public: 
    // get min point values
    bool        get_MinXYZ(double& X, double& Y, double& Z);
    // get max point values
    bool        get_MaxXYZ(double& X, double& Y, double& Z);
public:
// ---- Public member functions ----
    // virtual routine to generate bounding box : http://wiki.mcneel.com/developer/curvebrepbox
    virtual BOOL computeBBox(const CRhinoObject* Object) = 0;
    // virtual routine to check bounding box intersection
    virtual bool BBoxIntersection(CiEPlg_BBox* pOtherBBox) = 0;
    // routine for visualization
    BOOL doVisualBBox();
};
#endif /* CIEPLG_BBOX_HEADER */

In a previous post, you wrote:

Without seeing your code, I can only guess. But since “our corner points
are given as pointers”, my guess is that “my_Point_000”,
“my_Point_100”, etc. are declared as follows:

So I think, that the corners points are defined in the header file TGFRhinoPlugin_BBox.h as protected variables in the class CiEPlg_BBox like this:

class CiEPlg_BBox 
{
public:
// ---- Public variables ----
    BBoxType                gl_BBoxType;
    // epsilon of bboxes
    double                    gl_epsval;
    // half length distances
    double                    gl_rx;
    double                    gl_ry;
    double                    gl_rz;
protected:
// ---- protected variables ----
    // bbox corner points in global coordinates    
    ON_3dPoint*                my_Point_000;
    ON_3dPoint*                my_Point_100;
    ON_3dPoint*                my_Point_110;
    ON_3dPoint*                my_Point_010;
    ON_3dPoint*                my_Point_001;
    ON_3dPoint*                my_Point_101;
    ON_3dPoint*                my_Point_111;
    ON_3dPoint*                my_Point_011;
    // center point
    ON_3dPoint*                my_CenterPoint;

Can I still do this?

This is already done by the destructor CiEPlg_BBox::~CiEPlg_BBox() in the file TGFRhinoPlugin_BBox.cxx, which looks like this:

/**
 * \file TGFRhinoPlugin_BBox.cxx
 * \brief Implementation file for the class CiEPlg_BBox
 */
/***************************************************************************
 ****                                                                   ****
 ****    Sources:    TGFRhinoPlugin_BBox.cxx                           ****
 ****                                                                   ****
 ***************************************************************************



/***************************************************************************
 **** Includes                                                          ****
 ***************************************************************************/
//--- Include Precompiled header
#include "StdAfx.h"

// ---- Internal Includes ---- 
#include "TGFRhinoPlugin_BBox.h"
#include "stdafx.h"
// ---- Other Includes ---- 




/***************************************************************************
 **** class CiEPlg_BBox : Constructor                                   ****
 ***************************************************************************/
CiEPlg_BBox::CiEPlg_BBox(BBoxType aBBoxType,double epsval)
{
    // init
    gl_BBoxType = aBBoxType;
    // nullify pointer to corner points    
    my_Point_000 = 0;
    my_Point_100 = 0;
    my_Point_110 = 0;
    my_Point_010 = 0;

    my_Point_001 = 0;
    my_Point_101 = 0;
    my_Point_111 = 0;
    my_Point_011 = 0;
    // center point init
    my_CenterPoint = 0;
    // eps value
    gl_epsval = epsval;
    // half length distances
    gl_rx = MFLOAT;
    gl_ry = MFLOAT;
    gl_rz = MFLOAT;
}


/***************************************************************************
 **** class CiEPlg_BBox : Destructor                                    ****
 ***************************************************************************/
/**
 * \fn ~CiEPlg_BBox()
 * \brief Destructor of the class CiEPlg_BBox
 */
CiEPlg_BBox::~CiEPlg_BBox()
{
    // delete dynamic elements
    delete my_Point_000;
    delete my_Point_100;
    delete my_Point_110;
    delete my_Point_010;

    delete my_Point_001;
    delete my_Point_101;
    delete my_Point_111;
    delete my_Point_011;

    delete my_CenterPoint;
}


/***************************************************************************
 **** class CiEPlg_BBox : member routine                                 ****
 ***************************************************************************/
BOOL CiEPlg_BBox::doVisualBBox()
{
    // check validity
    if(my_Point_000 == 0 || my_Point_100 == 0 || my_Point_110 == 0 || my_Point_010 == 0 ||
        my_Point_001 == 0 || my_Point_101 == 0 || my_Point_111 == 0 || my_Point_011 == 0) {
            return CRhinoCommand::failure;
    }

    CRhinoDoc* doc = RhinoApp().ActiveDoc();

    // ********* YOUR JOB ***

    // a ON_BrepBox shall be created and visualized in Rhino.
    // the box is defined by 8 corner points as you see in the lines above.
    // how to do that is shown also here: http://wiki.mcneel.com/developer/sdksamples/addbrepbox
    // Be aware that our corner points are given as pointers!

    // ********* YOUR JOB ***

    
  // define the corners of the box

CRhinoCommand::result rc = CRhinoCommand::nothing;

ON_3dPointArray boxcorners;
boxcorners.Append(*my_Point_000);
boxcorners.Append(*my_Point_100);
boxcorners.Append(*my_Point_110);
boxcorners.Append(*my_Point_010);
boxcorners.Append(*my_Point_001);
boxcorners.Append(*my_Point_101);
boxcorners.Append(*my_Point_111);
boxcorners.Append(*my_Point_011);

  // Build the brep  

  ON_Brep* pBrep = ON_BrepBox( boxcorners );
  if( pBrep )
  {
    CRhinoBrepObject* pObject = new CRhinoBrepObject();
    pObject->SetBrep( pBrep );
    if( doc->AddObject(pObject) )
    {
      doc->Redraw();
      rc = CRhinoCommand::success;
    }
    else
    {
      delete pObject;
      pObject = 0;
      rc = CRhinoCommand::failure;
    }
  }
  return rc;

}

/***************************************************************************
 **** class CiEPlg_BBox : member routine                                 ****
 ***************************************************************************/
bool CiEPlg_BBox::get_MinXYZ(double& X, double& Y, double& Z)
{
    // check validity
    if(my_Point_000 == 0 || my_Point_100 == 0 || my_Point_110 == 0 || my_Point_010 == 0 ||
        my_Point_001 == 0 || my_Point_101 == 0 || my_Point_111 == 0 || my_Point_011 == 0) {
            return false;
    }

    // return values
    X = my_Point_000->x;
    Y = my_Point_000->y;
    Z = my_Point_000->z;

    // return 
    return true;
}


/***************************************************************************
 **** class CiEPlg_BBox : member routine                                 ****
 ***************************************************************************/
bool CiEPlg_BBox::get_MaxXYZ(double& X, double& Y, double& Z)
{
    // check validity
    if(my_Point_000 == 0 || my_Point_100 == 0 || my_Point_110 == 0 || my_Point_010 == 0 ||
        my_Point_001 == 0 || my_Point_101 == 0 || my_Point_111 == 0 || my_Point_011 == 0) {
            return false;
    }

    // return values
    X = my_Point_111->x;
    Y = my_Point_111->y;
    Z = my_Point_111->z;

    // return 
    return true;
}

OK, I am totally lost here.

If you have code you’d like us to review, please upload (rather than just copying and pasting) as part of your reply. You can attach files to your reply by poking the upload button.

If you have small snippets of code you want to display, then use markdown syntax to format your code blocks.

For example, doing this:

```
ON_3dPoint* my_Point_000 = new ON_3dPoint(0.0, 0.0, 0.0);
ON_3dPoint* my_Point_100 = new ON_3dPoint(1.0, 0.0, 0.0);
ON_3dPoint* my_Point_110 = new ON_3dPoint(1.0, 1.0, 0.0);
ON_3dPoint* my_Point_010 = new ON_3dPoint(0.0, 1.0, 0.0);
ON_3dPoint* my_Point_001 = new ON_3dPoint(0.0, 0.0, 1.0);
ON_3dPoint* my_Point_101 = new ON_3dPoint(1.0, 0.0, 1.0);
ON_3dPoint* my_Point_111 = new ON_3dPoint(1.0, 1.0, 1.0);
ON_3dPoint* my_Point_011 = new ON_3dPoint(0.0, 1.0, 1.0);
```

Will result in this:

ON_3dPoint* my_Point_000 = new ON_3dPoint(0.0, 0.0, 0.0);
ON_3dPoint* my_Point_100 = new ON_3dPoint(1.0, 0.0, 0.0);
ON_3dPoint* my_Point_110 = new ON_3dPoint(1.0, 1.0, 0.0);
ON_3dPoint* my_Point_010 = new ON_3dPoint(0.0, 1.0, 0.0);
ON_3dPoint* my_Point_001 = new ON_3dPoint(0.0, 0.0, 1.0);
ON_3dPoint* my_Point_101 = new ON_3dPoint(1.0, 0.0, 1.0);
ON_3dPoint* my_Point_111 = new ON_3dPoint(1.0, 1.0, 1.0);
ON_3dPoint* my_Point_011 = new ON_3dPoint(0.0, 1.0, 1.0);

I’ve gone back and edited your posts so they are more readable.

1 Like

Heyyyy, that’s exactly, what I wanted to know. If I would have known THIS earlier…

Thank you, in future I will upload the files instead of just copying and pasting the code from the files.

Sorry for the work, that I caused you. You won’t have to edit my posts again. I will do this :slight_smile:

This is the project structure:

So … Life isn’t easy and I am pretty much lost here also…

help

And which Format should the code have? Uploading a .cpp file isn’t working…