Control Points on a Bézier Curve

Hello mates,

I'm new to openCASCADE, I have been making a bit of research for the last past weeks to explore the possibilities of this wonderfull open source Kernel. I've also been diving trhough the forums to see if my issue was previously commented and although, many post contain the same theme, I haven't come across any which actually helped me.
Unfortunatelly I have also just started learning c++ on top of my previous c acknowledge, which difficults the process.

Well, here's my issue:
I would like to generate 3d objects based on Bézier's curves which control points could be modified by the user by dragging them on the GUI.
The underliying idea is quite simple, with a Béier curve that's contained in a 2d plane and which control points are susceptible of modification, the program must generate a whole 3d object by revolving this curve contained on that very first plane arround the Z axis.

So my questions are:
-Is this feasable on CASCADE? (Although, I actually belive that the answer is yes, I want to be sure)
-Which would be the easiest way of approaching this idea?
-Is it there any already writen code on Open CASCADE, or in it's 3rdparty add-ons, that do exactly this?
-Is the Bézier curve idea the best way of working with this issue? Would it be better to go for a NURBS oriented implementation? Would it be even easier with a Bspline?

Hopefuly, I haven't overextended much myself, and someone is able to contribute to my case.

Thanks for the long reading, Best Regards

Pawel's picture

Hi Jaume,

-Is this feasable on CASCADE? (Although, I actually belive that the answer is yes, I want to be sure)
Yes, it is. OCCT will offer you all the means you need to accomplish the task. So why not ;)

-Which would be the easiest way of approaching this idea?
I think that depends on your knowledge of OCCT and possibly other products. For example, some people use only the geometrical modeling possibilities from the kernel. They handle GUI, file saving, the undo/redo mechanism with some other products/methods. So I guess you have to specify what kind of application / operating system you target at.

-Is it there any already writen code on Open CASCADE, or in it's 3rdparty add-ons, that do exactly this?
You might want to have a look at FreeCAD and some other similar projects based on OCCT. They might offer what you want.

-Is the Bézier curve idea the best way of working with this issue? Would it be better to go for a NURBS oriented implementation? Would it be even easier with a Bspline?
Again, this depends on what/how you want to achieve. Why have you decided on Bezier so far? Anyway, you can handle all the mentioned curve types with OCCT.

Hope this helps
Pawel

ezno's picture

Hi Pawel,

Thank you very much for your quick answer.
Let me draw a bit the background of my idea, I am employed on the R+D department on fluid machinery company. For the last 15 years we have been using a convination of Excel Workbooks and VBa to define the profiles and shapes of the pumps, to finally extract a model which is exported to CAD systems and finally processed on CAE.
Due to the sophisticated calculus exposed, the overall performance is no longer acceptable and we are hesitating different solutions. Summarazing, we are deciding wether or not it would be not really paintfull to start building our own personalized system.

So far, I am now moving forward from c to c++, I have faced the whole installation and rebuilding process for the OCCT, I dealed with the tutorials and the DRAW programm. Next step is not clear yet and was what I introducced in the previous post:
-Would it be more beneficiary an own built, self-sweated, tear-forged programm which would adapt to excactly we need
-Could we recycle some other previous code, adapting it to our requirements

Which are our needs:
-We need to have total control of Bézier curves ruled by KeyPoints
-We need a powerful tool that can generate solids out of curves
-We need a consistent Meshing tool, that should be governated by the designer instead of by some algorism

We run on 64bit Windows 7 enviroment.

Hopefully it's everything clear, and once again, someone can help me a bit

Thanks very much and Best Regards

Pawel's picture

Hi Jaume,

here some hints on the subject:

- OCCT has a pretty flat learning curve so make sure you plan enough time if you think to base a complex software project on that. However, I would still take OCCT rather than programming from scratch.

- You can generate solids based on curves. Whether you take Bezier curves or Nurbs doesn't make any difference.

- Meshing is problematic in OCCT. First of all, you can mesh only surfaces. Secondly, it can't be controlled very well. If you need 3D-Meshes for FEM then you have some possibilities:
a) have a look at the commercial products offered by OCC
b) there are two open source meshing projects (that I'm aware of) that integrate with OCCT: Netgen and gmesh

- Although I think the 64bit version (on Windows but not only) has still probably some problems it works quite well.

Pawel

ezno's picture

Hello Pawel,

Sorry for the late reply, yesterday was a really busy day at the office and I had no time to revise the forum.

Your tips are very usefull, considering what we have seen thus far, OCCT seems to contain all the tools that we need.

Regarding to the meshing subject, as you stated, there are already some open source solutions that bare with it and integrate perfectly under the OCCT software.

Now I am concentrating my efforts on developing that initial tool which in a draft manner works with ControlPoints and generate a B-Curve. Eventually we will come up with a final solution.

Thanks you very much for all your contribution.

Kind Regards

JuryS's picture

Try something like this:

Handle(Geom_BSplineCurve) SPL1 =
GeomAPI_PointsToBSpline(array).Curve();

BRep_Builder Brep;
TopoDS_Edge& E = TopoDS::Edge(ResultShape);

Brep.MakeEdge(E,SPL1,1.e-7);

TopoDS_Vertex Vfirst,Vend;
Brep.MakeVertex(Vfirst,array.Value(1),1.e-7);
Brep.MakeVertex(Vend,array.Value(aCurrent-1),1.e-7);

Vfirst.Orientation(TopAbs_FORWARD);
Vend.Orientation(TopAbs_REVERSED);

Brep.Add(E,Vfirst); //First point

for (int i = 2; i < (aCurrent-1); ++i)
{
TopoDS_Vertex V;
Brep.MakeVertex(V,array.Value(i),1.e-7);
Brep.Add(E,V);
}

Brep.Add(E,Vend); //Last point

After you can read all points from spline with TopEXP_Explorer like this:
for (TopExp_Explorer myPointExplorer(aShape,TopAbs_VERTEX); myPointExplorer.More(); myPointExplorer.Next())
{
vertex = TopoDS::Vertex(myPointExplorer.Current());
pt = BRep_Tool::Pnt(vertex);

aResult.append(pt.X());
aResult.append(pt.Y());
aResult.append(pt.Z());
}

And you need to remove control points with next before you operate with spline:
TopoDS_Vertex vertex;
BRep_Builder Brep;
TopoDS_Shape resultShape(aShape);

TopExp_Explorer myPointExplorer(aShape,TopAbs_VERTEX);

myPointExplorer.Next(); //No first!

while (myPointExplorer.More())
{
vertex = TopoDS::Vertex(myPointExplorer.Current());
myPointExplorer.Next();

if (myPointExplorer.More()) //Not last!
Brep.Remove(resultShape,vertex);

ezno's picture

Hello Yuriy,

Sorry for not having answered yet. Now's time to work hard on it.

As a first trial, I have rawly added your code to the sample provided in the OpenCascade sample Qf, compilling, generating a new exe and executing the programm, but it havent worked yet, it seems that the code doesnt get implemented in the new exe. So until now I haven't succeded in testing your code.

Although I'm looking to find a way in implementing your code to the current example and executing it to give it a try and see wether or not it's what i was looking for.

Maybe you can provide me with some hints or tips in the subject.

Thank you very much for your effort, you have been really helpful.

Kind Regards

Pierre Juillard's picture

Hi Jaume,

Only as indication, you can find such a feature in Salome \ GEOM module (based on OCCT).
Look at the help page here: it is indicated how you can create Bezier curves based on control points:
http://docs.salome-platform.org/salome_6_5_0/gui/GEOM/create_curve_page....

If this fits your need, but you need to specialize Salome interface for your own needs, there is a plug-in mechanisms with which you can create business-specific interfaces with Qt and python.
With the python API you have also full access to all the geometry and meshing commands/parameters of Salome, and you can developp specific algorithms matching your own needs.

I would think this is the shortest way to do, and also the less costly, because you rely on an exisitng application with its own GUI and so own.

PS: if you target optimization application, this is the way to go:
- the Salome notebook allows you to manage optimization parameters like point coordinates and so on...
- the YACS module allows you managing the different numerical tasks for your optimization loop and distributing them on clusters (geometry generation and update, meshing, numerical computations, post-processing...)

Bests,

Pierre

Salome : http://www.salome-platform.org/

ezno's picture

Hi Pierre,

Salome is a great programm. I have been investigating a bit in the features it has and I have found that is really easy to use yet quite powerful.
More specifically, on this precise module, \GEOM, the possibility to manage the KP on B-Curves is the idea I was looking for. Unfortunatelly, those are not movable by the user on the screen, they are fixed KP on a fixed axis. Here's where the specialization beggins, I would need to be able to add or modify that.

I would like to apologize because I haven't understood you completely. Correct me if I'm wrong; Salome code is accessible through python API, which shows the source code of the software. The code is written in phyton language and so have to be the consecutive mods.

Another question, what do you refere by the Salome Notebook? Do you mean the in-built phyton console? I have tried to insert code on it, as to create B-Curves for example and not succeded.

YACS module is a great tool for multiphysics simulation and will securely be very helpfull, however, I would now concentrate on the first stage, leaving this to posterior development.

Thank you very much for your hints and time

Best Regards

Pierre Juillard's picture

Jaume,

You are right, I don't think it is possible for Salome to update in real-time Bezier curve as the user move KPs.
I guess that this would require specific developments.

On the other hand, if you don't need real time, Salome already provides an update procedure through its notebook component described here:
http://docs.salome-platform.org/salome_5_1_6/geom/user/using_notebook_ge...

Basically, the notebook allows the user tagging specifc parameters as variables, and modifying their values later during the studies.
For instance, the x, y, and z coordinates of a KP.

The user can thus freely modify the tagged values (stored in the notebook), and update its study ("update study" button): the Bezier curve will then be updated accordingly to the changes.

Different kind of parameters can be tagged: from GEOM module, and also SMESH module.

"The code is written in phyton language and so have to be the consecutive mods."

The code is written in C++ and wrapped in python: it has a C++ API and a python API.
Developping in python is faster than C++, so developping business-oriented plug-in is the correct logic.
However, generic and CPU-intensive algorithms are developped in C++ and made available then through the python API by C++ code wrapping.

It is also possible to developp modules or plug-ins in C++.
The interest to do it in Salome is that you will be able to re-use Salome tools (viewers...) and have your module available to be used in YACS.

Do not hesitate to go deeper in Salome.
It is very powerful, and it will certainly save you time later on, except if your target a "real-time" application that reacts in live to user modifications.

Bests,

Pierre

ezno's picture

Hello Pierre,

Sorry for the late reply, at the weekend I am not at the office. All your guidance has so far being crystal clear, and helped me clarify my ideas.

I have been working with the Salome Notebook, setting variables and modifying them by the "update" button, and all worked really smoth, from the KP's on the B-curve to the finall 3d revolved solid. But it still is not what I am looking for, as you suggested, the idea of real time modification will only be available under a strong code development. Although, I am still not sure if Salome's libraries will let me do that.

Thank you for straighten out the confusion with the actuall language of the code. I am still not familiar with the terminology.
If I undestood it correctly, developers can write code both in Python and C++, but writing "ligh code" in phyton speeds up the process due to its low complexity. So, if I want to scrip an own function which for example deals with reading KP and creating a B-curve from them a quick ten-lines code in phyton can be writen; although, if my intention is to modify the libraries Salome source code I should work with C++.

I still have many doubts concerning Open Cascade and Salome, may you or someone else be so kind to help me solve them.
Here are some of my doubts:

-Open Cascade provides us with OCAF, a developing framework which contains data and functionallities that are intended to help the forthcoming programmers to develop structured applications. Basically, it makes easier the task of adding functions to the existent code because it has certain definitions and approaches to the data that the CAD systems manage which enhances the productivity of the developer.
The question is: Is that all correct? Did I missunderstood something? Which is the propper way to start using OCAF?

-Using Salome, let me picture the background of the idea:
The user will have a 2d plane where in a Cartisean CS there will be represented a sketch of the solid, which will contain its different parts, some of them would be B-curves governated by KP (the way of managing the KP's, it is not still clear, maybe through the notebook or another different method). After revising and adapting the sketch with a button, the programm will generate the whole solid and using other Open Cascade libraries it would be imported to some specific format.
The question is: Where should I start to work on it? Is there any other way of approaching the idea which is easier in terms of development?

Once again, thank you very much for your time and expertise, sincerely Open Cascade is a vast universe to discover and wihtout guidance man can run in circles.

Kind Regards

JuryS's picture

With Bezier curve more simply. I think before that you can't get points from spline curve. With bezier curve you can read this points with:

QVector SameTools::getCoordFromBezier(const TopoDS_Shape &aShape)
{
gp_Pnt pt;
QVector aResult;

TopoDS_Edge aEdge = TopoDS::Edge(aShape);
double first, last;
Handle(Geom_Curve) crv = BRep_Tool::Curve(aEdge, first, last);
Handle(Geom_BezierCurve) bez = Handle(Geom_BezierCurve)::DownCast(crv);

for (int i = 1; i <= bez->NbPoles(); ++i)
{
pt = bez->Pole(i);

aResult.append(pt.X());
aResult.append(pt.Y());
aResult.append(pt.Z());
}

return aResult;
}

If you want to edit Bezier curve you must read previous values, then remove curve and use markers to define new points location. Make some actions in your viewer, in my case this action named CurAction3d_EditPath. Here I move markers and create new curves with used points.

ezno's picture

Hello Yuriy,

I appreciate really much your help, and I think I understood the method that your purposed me.
The idea is to each time the user modifies the values those must be read and generated in a new B-curve deleting the older one. The code is quite clear, the problem is that I do not manage to get it to work. I have tried modifiying the previous files that are provided in the samples, in MFC, then recompiling and executing the programm, but no changes are made and the code that I just added seems to do not get to the screen.

I would like to request some help in the process, where do you exactly make this changes? Or where are you exactly working on? How should I work as to identify and modify code that is relevant to my interests?

Thank you very much for your help.

Kind Regards

JuryS's picture

If you only modified MFC sample:

//first time to display:
Handle_AIS_Shape directionalLine = new AIS_Shape(Created_Bezier);
myContext->SetColor(directionalLine,Quantity_NOC_BLUE3,false);
myContext->Display(directionalLine,0,-1);

//or redisplay if you modified previous like directionalLine->Set(Bezier);
myContext->Redisplay(directionalLine,true,false);

It's a OpenCascade libraries features. Here is more classes and here we have classes for analysis your bezier curve, then we have classes for create new curve. In the last you may use classes for displaying your result. All of this classes come with Toolkits. All of this toolkits contains in modules:

1. Module FoundationClasses
2. Module ModelingData
3. Module ModelingAlgorithms
4. Module Visualization
Module ApplicationFramework
Module DataExchange
Module Draw

In your case you must use first four modules:
1-2. Analize previous curve
2-3. Create new curve
4. Display your result.

Don't forget that here you are use Module Visualization. But in the feature you may use ApplicationFramework (document interface) that contains labels(tags) for all of objects and presentation. And in the feature you may use more features.