1 // VISU OBJECT : interactive object for VISU entities implementation
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
23 // File : VISU_PrsObject_i.cxx
24 // Author : Alexey PETROV
27 #include "VISU_CutLinesPL.hxx"
28 #include "VISU_Result_i.hh"
29 #include "VISU_CutLines_i.hh"
30 #include "VISU_Actor.h"
32 #include "VISU_PipeLineUtils.hxx"
34 #include <vtkAppendPolyData.h>
39 static float EPS_machine = 1.0E-7;
42 static int MYDEBUG = 0;
44 static int MYDEBUG = 0;
47 int VISU::CutLines_i::IsPossible(Result_i* theResult, const char* theMeshName, VISU::Entity theEntity,
48 const char* theFieldName, int theIteration, int isMemoryCheck)
50 return VISU::ScalarMap_i::IsPossible(theResult,theMeshName,theEntity,theFieldName,theIteration,isMemoryCheck);
53 int VISU::CutLines_i::myNbPresent = 0;
54 QString VISU::CutLines_i::GenerateName() { return VISU::GenerateName("CutLines",myNbPresent++);}
56 const string VISU::CutLines_i::myComment = "CUTLINES";
57 const char* VISU::CutLines_i::GetComment() const { return myComment.c_str();}
61 CutLines_i(Result_i* theResult,
63 PrsObject_i(theResult->GetStudyDocument()),
64 Prs3d_i(theResult,theAddToStudy),
65 ScalarMap_i(theResult,theAddToStudy),
72 CutLines_i(Result_i* theResult,
73 SALOMEDS::SObject_ptr theSObject) :
74 PrsObject_i(theResult->GetStudyDocument()),
75 Prs3d_i(theResult,theSObject),
76 ScalarMap_i(theResult,theSObject),
82 void VISU::CutLines_i::SameAs(const CutLines_i* theOrigin){
83 VISU::ScalarMap_i::SameAs(theOrigin);
87 VISU::Storable* VISU::CutLines_i::Create(const char* theMeshName, VISU::Entity theEntity,
88 const char* theFieldName, int theIteration)
90 return ScalarMap_i::Create(theMeshName,theEntity,theFieldName,theIteration);
94 VISU::Storable* VISU::CutLines_i::Restore(const Storable::TRestoringMap& theMap)
97 SetNbLines(VISU::Storable::FindValue(theMap,"myNbLines").toInt());
98 SetDisplacement(VISU::Storable::FindValue(theMap,"myDisplacement[0]").toDouble());
99 SetDisplacement2(VISU::Storable::FindValue(theMap,"myDisplacement[1]").toDouble());
100 SetOrientation(CutPlanes::Orientation(VISU::Storable::FindValue(theMap,"myBasePlane[0]").toInt()),
101 Storable::FindValue(theMap,"aRot[0][0]").toDouble(),
102 Storable::FindValue(theMap,"aRot[0][1]").toDouble());
103 SetOrientation2(CutPlanes::Orientation(VISU::Storable::FindValue(theMap,"myBasePlane[1]").toInt()),
104 Storable::FindValue(theMap,"aRot[1][0]").toDouble(),
105 Storable::FindValue(theMap,"aRot[1][1]").toDouble());
106 if (VISU::Storable::FindValue(theMap,"myBasePlaneCondition").toInt())
109 SetBasePlanePosition(VISU::Storable::FindValue(theMap,"myBasePlanePosition").toDouble());
111 QStringList aPosList = QStringList::split("|",VISU::Storable::FindValue(theMap,"myLinePosition") );
112 QStringList aCondList = QStringList::split("|",VISU::Storable::FindValue(theMap,"myLineCondition") );
113 for(int i = 0, iEnd = GetNbLines(); i < iEnd; i++)
114 if(aCondList[i].toInt() == 0)
115 SetLinePosition(i,aPosList[i].toDouble());
117 return ScalarMap_i::Restore(theMap);
121 void VISU::CutLines_i::ToStream(std::ostringstream& theStr){
122 ScalarMap_i::ToStream(theStr);
124 int aNbLines = GetNbLines();
126 Storable::DataToStream( theStr, "myNbLines", aNbLines );
127 Storable::DataToStream( theStr, "myDisplacement[0]", GetDisplacement() );
128 Storable::DataToStream( theStr, "myDisplacement[1]", GetDisplacement2() );
129 Storable::DataToStream( theStr, "myBasePlane[0]", int(GetOrientationType()) );
130 Storable::DataToStream( theStr, "myBasePlane[1]", int(GetOrientationType2()) );
131 Storable::DataToStream( theStr, "aRot[0][0]", GetRotateX() );
132 Storable::DataToStream( theStr, "aRot[0][1]", GetRotateY() );
133 Storable::DataToStream( theStr, "aRot[1][0]", GetRotateX2() );
134 Storable::DataToStream( theStr, "aRot[1][1]", GetRotateY2() );
135 Storable::DataToStream( theStr, "myBasePlanePosition", GetBasePlanePosition() );
136 Storable::DataToStream( theStr, "myBasePlaneCondition", IsDefault() );
138 QString aStrPos, aStrCon;
139 for(int i = 0, iEnd = GetNbLines(); i < iEnd; i++){
140 aStrPos.append(QString::number(GetLinePosition(i)) + "|");
141 aStrCon.append(QString::number(IsDefaultPosition(i)) + "|");
143 Storable::DataToStream( theStr, "myLinePosition", aStrPos.latin1());
144 Storable::DataToStream( theStr, "myLineCondition", aStrCon.latin1());
147 VISU::CutLines_i::~CutLines_i(){
148 if(MYDEBUG) MESSAGE("CutLines_i::~CutLines_i()");
152 void VISU::CutLines_i::SetOrientation(VISU::CutPlanes::Orientation theOrient,
153 CORBA::Double theXAngle, CORBA::Double theYAngle)
155 myCutLinesPL->SetOrientation(VISU_CutPlanesPL::PlaneOrientation(theOrient),
156 theXAngle,theYAngle);
158 void VISU::CutLines_i::SetOrientation2(VISU::CutPlanes::Orientation theOrient,
159 CORBA::Double theXAngle, CORBA::Double theYAngle)
161 myCutLinesPL->SetOrientation(VISU_CutPlanesPL::PlaneOrientation(theOrient),
162 theXAngle,theYAngle,1);
165 VISU::CutPlanes::Orientation VISU::CutLines_i::GetOrientationType() {
166 return VISU::CutPlanes::Orientation(myCutLinesPL->GetPlaneOrientation());
168 VISU::CutPlanes::Orientation VISU::CutLines_i::GetOrientationType2() {
169 return VISU::CutPlanes::Orientation(myCutLinesPL->GetPlaneOrientation(1));
172 CORBA::Double VISU::CutLines_i::GetRotateX(){
173 return myCutLinesPL->GetRotateX();
175 CORBA::Double VISU::CutLines_i::GetRotateY(){
176 return myCutLinesPL->GetRotateY();
179 CORBA::Double VISU::CutLines_i::GetRotateX2(){
180 return myCutLinesPL->GetRotateX(1);
182 CORBA::Double VISU::CutLines_i::GetRotateY2(){
183 return myCutLinesPL->GetRotateY(1);
187 void VISU::CutLines_i::SetDisplacement(CORBA::Double theDisp) {
188 myCutLinesPL->SetDisplacement(theDisp);
190 void VISU::CutLines_i::SetDisplacement2(CORBA::Double theDisp) {
191 myCutLinesPL->SetDisplacement(theDisp,1);
194 CORBA::Double VISU::CutLines_i::GetDisplacement() {
195 return myCutLinesPL->GetDisplacement();
197 CORBA::Double VISU::CutLines_i::GetDisplacement2() {
198 return myCutLinesPL->GetDisplacement(1);
202 void VISU::CutLines_i::SetBasePlanePosition(CORBA::Double thePlanePosition){
203 myCutLinesPL->SetPosition(thePlanePosition);
206 CORBA::Double VISU::CutLines_i::GetBasePlanePosition(){
207 return myCutLinesPL->GetPosition();
210 void VISU::CutLines_i::SetLinePosition(CORBA::Long thePlaneNumber, CORBA::Double thePlanePosition){
211 myCutLinesPL->SetPartPosition(thePlaneNumber,thePlanePosition);
214 CORBA::Double VISU::CutLines_i::GetLinePosition(CORBA::Long thePlaneNumber){
215 return myCutLinesPL->GetPartPosition(thePlaneNumber,1);
219 void VISU::CutLines_i::SetDefault(){
220 myCutLinesPL->SetDefault();
223 CORBA::Boolean VISU::CutLines_i::IsDefault(){
224 return myCutLinesPL->IsDefault();
227 void VISU::CutLines_i::SetDefaultPosition(CORBA::Long thePlaneNumber){
228 myCutLinesPL->SetPartDefault(thePlaneNumber);
231 CORBA::Boolean VISU::CutLines_i::IsDefaultPosition(CORBA::Long thePlaneNumber){
232 return myCutLinesPL->IsPartDefault(thePlaneNumber);
236 void VISU::CutLines_i::SetNbLines(CORBA::Long theNb) {
237 myCutLinesPL->SetNbParts(theNb);
239 CORBA::Long VISU::CutLines_i::GetNbLines() {
240 return myCutLinesPL->GetNbParts();
244 void VISU::CutLines_i::DoHook(){
245 if(!myPipeLine) myPipeLine = VISU_CutLinesPL::New();
246 myCutLinesPL = dynamic_cast<VISU_CutLinesPL*>(myPipeLine);
248 ScalarMap_i::DoHook();
251 void VISU::CutLines_i::BuildTableOfReal(SALOMEDS::SObject_ptr theSObject){
253 if(MYDEBUG) MESSAGE("CutPlanes_i::BuildTableOfReal");
255 SALOMEDS::GenericAttribute_var anAttr;
256 SALOMEDS::StudyBuilder_var aStudyBuilder = myStudy->NewBuilder();
257 anAttr = aStudyBuilder->FindOrCreateAttribute(theSObject, "AttributeTableOfReal");
258 SALOMEDS::AttributeTableOfReal_var aTableOfReal = SALOMEDS::AttributeTableOfReal::_narrow(anAttr);
260 typedef set<float> TXCont;
261 typedef map<float,float> TXYMap;
262 typedef map<int,TXYMap> TXYMapCont;
263 typedef map<long,long> TLineIdCont;
266 aTitle.sprintf("%s",myTitle.c_str());
267 aTitle = aTitle.simplifyWhiteSpace();
268 aTableOfReal->SetTitle(aTitle.latin1());
270 int iLineEnd = myCutLinesPL->GetAppendPolyData()->GetNumberOfInputs();
271 if(MYDEBUG) MESSAGE("CutPlanes_i::BuildTableOfReal iLineEnd = "<<iLineEnd);
273 TXYMapCont aXYMapCont;
274 TLineIdCont aLineIdCont; // Define internal numeration of lines
275 const float *aDirLn = myCutLinesPL->GetDirLn();
276 const float *aBasePnt = myCutLinesPL->GetBasePnt();
277 const float *aBoundPrjLn = myCutLinesPL->GetBoundPrjLn();
278 for(int iLine = 0, jLine = 0; iLine < iLineEnd; iLine++){
279 vtkDataSet *aDataSet = myCutLinesPL->GetAppendPolyData()->GetInput(iLine);
281 int aNbPoints = aDataSet->GetNumberOfPoints();
282 if(!aNbPoints) continue;
283 vtkPointData *aPointData = aDataSet->GetPointData();
284 vtkDataArray *aScalars = aPointData->GetScalars();
285 vtkCellDataToPointData *aFilter = NULL;
287 aFilter = vtkCellDataToPointData::New();
288 aFilter->SetInput(aDataSet);
289 aFilter->PassCellDataOn();
290 aDataSet = aFilter->GetOutput();
293 aPointData = aDataSet->GetPointData();
294 aScalars = aPointData->GetScalars();
295 if(!aScalars) continue;
296 if(MYDEBUG) MESSAGE("CutPlanes_i::BuildTableOfReal iLine = "<<iLine<<"; aNbPoints = "<<aNbPoints);
297 aLineIdCont[iLine] = jLine++;
298 TXYMap& aXYMap = aXYMapCont[iLine];
299 float aPnt[3], aVect[3], aDist;
300 for(int i = 0; i < aNbPoints; i++){
301 aDataSet->GetPoint(i,aPnt);
302 Sub(aPnt,aBasePnt,aVect);
303 if ( fabs(aBoundPrjLn[2]) < EPS_machine )
307 aDist = vtkMath::Dot(aVect,aDirLn) / aBoundPrjLn[2];
309 if(aDist < 0.0) aDist = 0.0;
310 if(aDist > 1.0) aDist = 1.0;
312 aXYMap[aDist] = aScalars->GetTuple1(i);
315 aFilter->UnRegisterAllOutputs();
319 if(aXYMapCont.size() == 0)
320 throw std::runtime_error("CutPlanes_i::BuildTableOfReal aXYMapCont.size() == 0 !!!");
321 //Resorting of theXYMap
322 TXYMapCont::iterator aXYMapContIter = aXYMapCont.begin();
323 for(; aXYMapContIter != aXYMapCont.end(); aXYMapContIter++){
324 TXYMap& aXYMap = aXYMapContIter->second, aNewXYMap;
325 if(aXYMap.size() > 2){
326 // Try to smooth the values of the line by applying linear approximation
327 TXYMap::const_iterator aXYMapIter[2] = {aXYMap.begin(), ++aXYMap.begin()};
328 aNewXYMap[aXYMapIter[0]->first] = aXYMapIter[0]->second;
329 aXCont.insert(aXYMapIter[0]->first);
330 for(; aXYMapIter[1] != aXYMap.end(); aXYMapIter[0]++, aXYMapIter[1]++){
331 float aY[3] = {aXYMapIter[0]->second, aXYMapIter[1]->second, 0.0};
332 aY[2] = (aY[0] + aY[1])/2.0;
333 float aX[3] = {aXYMapIter[0]->first, aXYMapIter[1]->first, 0.0};
334 aX[2] = (aX[0] + aX[1])/2.0;
335 aNewXYMap[aX[2]] = aY[2];
336 aXCont.insert(aX[2]);
338 aNewXYMap[aXYMapIter[0]->first] = aXYMapIter[0]->second;
339 aXCont.insert(aXYMapIter[0]->first);
342 TXYMap::const_iterator aXYMapIter = aXYMap.begin();
343 for(; aXYMapIter != aXYMap.end(); aXYMapIter++)
344 aXCont.insert(aXYMapIter->first);
347 if(aXCont.size() == 0)
348 throw std::runtime_error("CutPlanes_i::BuildTableOfReal aXCont.size() == 0 !!!");
350 int iEnd = aXCont.size();
351 aTableOfReal->SetNbColumns(iEnd);
352 TXCont::const_iterator aXContIter = aXCont.begin();
353 for(long i = 0; aXContIter != aXCont.end(); aXContIter++, i++){
354 float aDist = *aXContIter;
355 aTableOfReal->PutValue(aDist,1,i+1);
356 aString.sprintf("%d",i);
357 aTableOfReal->SetColumnTitle(i+1,aString.latin1());
358 if(MYDEBUG) MESSAGE("CutPlanes_i::BuildTableOfReal aDist = "<<aDist);
359 TXYMapCont::const_iterator aXYMapContIter = aXYMapCont.begin();
360 for(; aXYMapContIter != aXYMapCont.end(); aXYMapContIter++){
361 long iLine = aXYMapContIter->first;
362 long iLineId = aLineIdCont[iLine];
363 const TXYMap& aXYMap = aXYMapCont[iLine];
364 TXYMap::const_iterator aXYMapIter = aXYMap.find(aDist);
365 // Can we find some value that belong to the line and have the same X coordinate?
366 if(aXYMapIter == aXYMap.end()) continue;
367 float aVal = aXYMapIter->second;
368 aTableOfReal->PutValue(aVal,iLineId+2,i+1);
372 aTableOfReal->SetRowTitle(1,"X");
373 aTableOfReal->SetRowUnit(1,"-");
374 QString aUnitName = myField->myUnitNames[0].c_str();
375 int aScalarMode = myCutLinesPL->GetScalarMode();
377 aUnitName = myField->myUnitNames[aScalarMode-1].c_str();
378 aUnitName = aUnitName.simplifyWhiteSpace();
379 if(aUnitName.isEmpty()) aUnitName = "-";
380 TXYMapCont::const_iterator aXYMapContIter = aXYMapCont.begin();
381 for(; aXYMapContIter != aXYMapCont.end(); aXYMapContIter++){
382 long iLine = aXYMapContIter->first;
383 long iLineId = aLineIdCont[iLine];
384 aString.sprintf("Y%d",iLine);
386 MESSAGE("CutPlanes_i::BuildTableOfReal - SetRowTitle("<<iLineId+2<<",'"<<aString<<"')");
387 aTableOfReal->SetRowTitle(iLineId+2,aString.latin1());
388 aTableOfReal->SetRowUnit(iLineId+2,aUnitName.latin1());
391 }catch(std::exception& exc){
392 INFOS("Follow exception was occured :\n"<<exc.what());
394 INFOS("Unknown exception was occured !!!");