fw4spl
SrcLib/tests/fwTest/src/fwTest/generator/Object.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwTest/generator/Object.hpp"
8 
9 #include "fwTest/generator/Image.hpp"
10 #include "fwTest/generator/Mesh.hpp"
11 #include "fwTest/generator/SeriesDB.hpp"
12 
13 #include <fwData/Integer.hpp>
14 #include <fwData/Plane.hpp>
15 #include <fwData/ProcessObject.hpp>
16 #include <fwData/Resection.hpp>
17 #include <fwData/ResectionDB.hpp>
18 #include <fwData/String.hpp>
19 
20 #include <fwMath/IntrasecTypes.hpp>
21 
22 #include <boost/filesystem/convenience.hpp>
23 #include <boost/filesystem/path.hpp>
24 
25 #include <cmath>
26 
27 namespace fwTest
28 {
29 namespace generator
30 {
31 
32 //------------------------------------------------------------------------------
33 
34 ::fwData::StructureTraitsDictionary::sptr Object::createStructureTraitsDictionary()
35 {
36  ::fwData::StructureTraitsDictionary::sptr structDico = ::fwData::StructureTraitsDictionary::New();
37  ::fwData::StructureTraits::sptr skin = ::fwData::StructureTraits::New();
38  std::string skinType = "Skin";
39  skin->setType(skinType);
40  skin->setClass(::fwData::StructureTraits::ENVIRONMENT);
41  ::fwData::Color::sptr skinColor = ::fwData::Color::New(255.0f/255.0f, 179.0f/255.0f, 140.0f/255.0f, 1.0f);
42  skin->setColor(skinColor);
43  ::fwData::StructureTraits::CategoryContainer skinCat(1);
44  skinCat[0] = ::fwData::StructureTraits::BODY;
45  skin->setCategories(skinCat);
46  structDico->addStructure(skin);
47 
48  ::fwData::StructureTraits::sptr liver = ::fwData::StructureTraits::New();
49  liver->setType("Liver");
50  liver->setClass(::fwData::StructureTraits::ORGAN);
51  liver->setColor(::fwData::Color::New(204.0f/255.0f, 51.0f/255.0f, 51.0f/255.0f, 1.0));
52  ::fwData::StructureTraits::CategoryContainer liverCat(1);
53  liverCat[0] = ::fwData::StructureTraits::ABDOMEN;
54  liver->setCategories(liverCat);
55  std::string nativeExp = "inter(world(type(Skin)),not(class(Organ)))";
56  liver->setNativeExp(nativeExp);
57  structDico->addStructure(liver);
58 
59  ::fwData::StructureTraits::sptr liverTumor = ::fwData::StructureTraits::New();
60  liverTumor->setType("Liver_Tumor");
61  liverTumor->setClass(::fwData::StructureTraits::LESION);
62  liverTumor->setColor(::fwData::Color::New(0.0f, 179.0f/255.0f, 0.0f, 1.0f));
63  ::fwData::StructureTraits::CategoryContainer liverTumorCat(1);
64  liverTumorCat[0] = ::fwData::StructureTraits::ABDOMEN;
65  liverTumor->setCategories(liverTumorCat);
66  liverTumor->setAttachmentType("Liver");
67  structDico->addStructure(liverTumor);
68 
69  ::fwData::StructureTraits::sptr tumor = ::fwData::StructureTraits::New();
70  tumor->setType("Tumor");
71  tumor->setClass(::fwData::StructureTraits::LESION);
72  tumor->setColor(::fwData::Color::New(0.0f, 0.0f, 1.0f, 1.0f));
73  ::fwData::StructureTraits::CategoryContainer tumorCat(8);
74  tumorCat[0] = ::fwData::StructureTraits::BODY;
75  tumorCat[1] = ::fwData::StructureTraits::HEAD;
76  tumorCat[2] = ::fwData::StructureTraits::NECK;
77  tumorCat[3] = ::fwData::StructureTraits::THORAX;
78  tumorCat[4] = ::fwData::StructureTraits::ABDOMEN;
79  tumorCat[5] = ::fwData::StructureTraits::PELVIS;
80  tumorCat[6] = ::fwData::StructureTraits::ARM;
81  tumorCat[7] = ::fwData::StructureTraits::LEG;
82  tumor->setCategories(tumorCat);
83 
84  ::fwData::StructureTraits::sptr colon = ::fwData::StructureTraits::New();
85  colon->setType("Colon");
86  colon->setClass(::fwData::StructureTraits::ORGAN);
87  colon->setColor(::fwData::Color::New(1.0f, 179.0f/255.0f, 128.0f/255.0f, 1.0f));
88  ::fwData::StructureTraits::CategoryContainer colonCat(1);
89  colonCat[0] = ::fwData::StructureTraits::ABDOMEN;
90  colon->setCategories(colonCat);
91  structDico->addStructure(colon);
92 
93  ::fwData::StructureTraits::sptr caecum = ::fwData::StructureTraits::New();
94  caecum->setType("Caecum");
95  caecum->setClass(::fwData::StructureTraits::FUNCTIONAL);
96  caecum->setColor(::fwData::Color::New(1.0f, 179.0f/255.0f, 128.0f/255.0f, 1.0f));
97  ::fwData::StructureTraits::CategoryContainer caecumCat(1);
98  caecumCat[0] = ::fwData::StructureTraits::ABDOMEN;
99  caecum->setCategories(caecumCat);
100  caecum->setAttachmentType("Colon");
101  structDico->addStructure(caecum);
102 
103  return structDico;
104 }
105 
106 //------------------------------------------------------------------------------
107 
108 ::fwData::StructureTraits::sptr Object::createStructureTraits()
109 {
110  ::fwData::StructureTraits::sptr pStructureTraits = ::fwData::StructureTraits::New();
111 
112  const std::string type = "Liver";
113  const ::fwData::StructureTraits::StructureClass structClass = ::fwData::StructureTraits::ORGAN;
114  const ::fwData::StructureTraits::ROIExpression nativeExp = "inter(world(type(Skin)),not(class(Organ)))";
115  const ::fwData::StructureTraits::ROIExpression nativeGeoExp = "halfSpace(world(Left_lung),Z+,true)";
116 
117  pStructureTraits->setType(type);
118  pStructureTraits->setClass(structClass);
119  pStructureTraits->setNativeExp(nativeExp);
120  pStructureTraits->setNativeGeometricExp(nativeGeoExp);
121 
122  ::fwData::Color::sptr color = ::fwData::Color::New();
123  color->setRGBA(rand()%100 / 100.f, rand()%100 / 100.f, rand()%100 / 100.f, rand()%100 / 100.f);
124  pStructureTraits->setColor(color);
125 
126  ::fwData::StructureTraits::CategoryContainer categories(2);
127  categories[0] = ::fwData::StructureTraits::ABDOMEN;
128  categories[1] = ::fwData::StructureTraits::BODY;
129  pStructureTraits->setCategories(categories);
130 
131  return pStructureTraits;
132 }
133 
134 //------------------------------------------------------------------------------
135 
136 ::fwData::Composite::sptr Object::createROITraits()
137 {
138  ::fwData::Composite::sptr composite = ::fwData::Composite::New();
139 
140  ::fwData::ROITraits::sptr roi = ::fwData::ROITraits::New();
141  std::stringstream ss;
142  ss << "ID_" << rand()%100;
143  roi->setIdentifier(ss.str());
144 
145  roi->setEvaluatedExp("inter(skin),not(liver)");
146  ::fwData::StructureTraits::sptr structure = Object::createStructureTraits();
147  roi->setStructureTraits(structure);
148 
149  ::fwData::Node::sptr maskNode = Object::createNode();
150  roi->setMaskOpNode(maskNode);
151 
152  (*composite)["ROITraits"] = roi;
153  (*composite)["StructureTraits"] = structure;
154  (*composite)["MaskOpNode"] = maskNode;
155 
156  return composite;
157 }
158 
159 //------------------------------------------------------------------------------
160 
161 ::fwData::Composite::sptr Object::createReconstructionTraits()
162 {
163  ::fwData::Composite::sptr composite = ::fwData::Composite::New();
164 
165  ::fwData::ReconstructionTraits::sptr rec = ::fwData::ReconstructionTraits::New();
166 
167  std::stringstream ss;
168  ss << "ID_" << rand()%100;
169  rec->setIdentifier(ss.str());
170 
171  ::fwData::StructureTraits::sptr structure = Object::createStructureTraits();
172  rec->setStructureTraits(structure);
173 
174  ::fwData::Node::sptr maskNode = Object::createNode();
175  rec->setMaskOpNode(maskNode);
176 
177  ::fwData::Node::sptr meshNode = Object::createNode();
178  rec->setMeshOpNode(meshNode);
179 
180  (*composite)["ReconstructionTraits"] = rec;
181  (*composite)["StructureTraits"] = structure;
182  (*composite)["MaskOpNode"] = maskNode;
183  (*composite)["MeshOpNode"] = meshNode;
184 
185  return composite;
186 }
187 
188 //------------------------------------------------------------------------------
189 
190 ::fwData::Node::sptr Object::createNode()
191 {
192  ::fwData::Node::sptr node = ::fwData::Node::New();
193 
194  unsigned int nbInputs = rand()%5;
195  unsigned int nbOutputs = rand()%5;
196 
197  for (unsigned int i = 0; i < nbInputs; ++i)
198  {
199  node->addInputPort(Object::createPort());
200  }
201 
202  for (unsigned int i = 0; i < nbOutputs; ++i)
203  {
204  node->addOutputPort(Object::createPort());
205  }
206 
207  return node;
208 }
209 
210 //------------------------------------------------------------------------------
211 
212 ::fwData::Port::sptr Object::createPort()
213 {
214  ::fwData::Port::sptr port = ::fwData::Port::New();
215 
216  std::stringstream ssId;
217  ssId << "ID_" << rand()%100;
218  port->setIdentifier(ssId.str());
219 
220  std::stringstream ssType;
221  ssType << "Type_" << rand()%100;
222  port->setType(ssType.str());
223 
224  return port;
225 }
226 
227 //------------------------------------------------------------------------------
228 
229 ::fwData::Color::sptr Object::randomizeColor()
230 {
231  ::fwData::Color::sptr col = ::fwData::Color::New();
232  col->setRGBA(rand()%100 / 100.f, rand()%100 / 100.f, rand()%100 / 100.f, rand()%100 / 100.f);
233  return col;
234 }
235 
236 //------------------------------------------------------------------------------
237 
238 ::fwData::TransferFunction::sptr Object::createTFColor(const unsigned char nbPoints,
239  const double window,
240  const double level)
241 {
242  ::fwData::TransferFunction::sptr tf = ::fwData::TransferFunction::New();
243 
244  tf->setBackgroundColor( ::fwData::TransferFunction::TFColor( rand()%100 / 100.f, rand()%100 / 100.f,
245  rand()%100 / 100.f, rand()%100 / 100.f) );
246  tf->setInterpolationMode( ::fwData::TransferFunction::NEAREST );
247  tf->setIsClamped( false );
248  tf->setLevel(level);
249  tf->setName( "TFColor" );
250  tf->setWindow(window );
251 
252  for (unsigned char nb = 0; nb < nbPoints; ++nb)
253  {
254  double value = rand()%100 - level;
255  tf->addTFColor( value,
256  ::fwData::TransferFunction::TFColor( rand()%100 / 100.f, rand()%100 / 100.f, rand()%100 / 100.f,
257  rand()%100 / 100.f ));
258  }
259 
260  ::fwData::String::sptr myString = ::fwData::String::New("fieldStringValue");
261  tf->setField( "fieldStringKey", myString );
262 
263  return tf;
264 }
265 
266 //------------------------------------------------------------------------------
267 
268 ::fwData::TransferFunction::sptr Object::createTFColor()
269 {
270 
271  ::fwData::TransferFunction::sptr tf = ::fwData::TransferFunction::New();
272 
273  tf->setBackgroundColor( ::fwData::TransferFunction::TFColor( 1.0f, 0.3f, 0.6f, 0.1f) );
274  tf->setInterpolationMode( ::fwData::TransferFunction::NEAREST );
275  tf->setIsClamped( false );
276  tf->setLevel( 900.6 );
277  tf->setName( "TFColor" );
278  tf->setWindow( -200.02 );
279 
280  tf->addTFColor( -40.33, ::fwData::TransferFunction::TFColor( 0.9f, 0.2f, 0.3f, 0.4f) );
281  tf->addTFColor( 3, ::fwData::TransferFunction::TFColor( 0.1f, 0.2f, 0.9f, 0.4f) ); // Invert point 3 <=> -0.2,
282  // for tests
283  tf->addTFColor( -0.2, ::fwData::TransferFunction::TFColor( 0.1f, 0.9f, 0.3f, 0.4f) );
284  tf->addTFColor( 150, ::fwData::TransferFunction::TFColor( 0.1f, 0.2f, 0.3f, 0.9f) );
285 
286  ::fwData::String::sptr myString = ::fwData::String::New("fieldStringValue");
287  tf->setField( "fieldStringKey", myString );
288 
289  return tf;
290 }
291 
292 //------------------------------------------------------------------------------
293 
294 ::fwData::Material::sptr Object::createMaterial()
295 {
296  // use the default value PHONG,SURFACE, STANDARD
297 
298  ::fwData::Color::sptr ambient = ::fwData::Color::New();
299  ambient->setRGBA(0.5f, 0.5f, 0.5f, 0.5f);
300 
301  ::fwData::Color::sptr diffuse = ::fwData::Color::New();
302  diffuse->setRGBA(0.8f, 0.2f, 0.5f, 0.4f);
303 
304  ::fwData::Material::sptr material = ::fwData::Material::New();
305 
306  material->setAmbient(::fwData::Object::copy( ambient ));
307  material->setDiffuse(::fwData::Object::copy( diffuse ));
308 
309  return material;
310 }
311 
312 //------------------------------------------------------------------------------
313 
314 ::fwData::ProcessObject::sptr Object::createProcessObject()
315 {
316  const std::string IMAGEID1 = "myImage1";
317  const std::string IMAGEID2 = "myImage2";
318  const std::string FIELDID1 = "myField1";
319  const std::string FIELDID2 = "myField2";
320  ::fwData::Image::sptr image1 = ::fwData::Image::New();
321  ::fwData::Image::sptr image2 = ::fwData::Image::New();
322  ::fwData::Integer::sptr field1 = ::fwData::Integer::New(3);
323  ::fwData::Integer::sptr field2 = ::fwData::Integer::New(8);
324 
325  // process
326  ::fwData::ProcessObject::sptr po = ::fwData::ProcessObject::New();
327  po->setInputValue(IMAGEID1, image1);
328  po->setInputValue(FIELDID1, field1);
329  po->setInputValue(FIELDID2, field2);
330  po->setOutputValue(IMAGEID2, image2);
331  return po;
332 }
333 
334 //------------------------------------------------------------------------------
335 
336 ::fwData::Point::sptr Object::generatePoint()
337 {
338  std::array< double, 3> coord = {static_cast<double>(rand()%300),
339  static_cast<double>(rand()%300),
340  static_cast<double>(rand()%300)};
341  ::fwData::Point::sptr point = ::fwData::Point::New();
342  point->setCoord(coord);
343  return point;
344 }
345 
346 //------------------------------------------------------------------------------
347 
348 ::fwData::Plane::sptr Object::generatePlane()
349 {
350  ::fwData::Plane::sptr plane = ::fwData::Plane::New();
351  plane->setValue(generatePoint(), generatePoint(), generatePoint());
352  plane->setIsIntersection(((rand()%1) ? true : false));
353  return plane;
354 }
355 
356 //------------------------------------------------------------------------------
357 
358 ::fwData::Resection::sptr Object::generateResection()
359 {
360 
361  ::fwData::Resection::sptr resection = ::fwData::Resection::New();
362 
363  resection->setName("Resection1");
364  resection->setIsSafePart(((rand()%1) ? true : false));
365  resection->setIsValid(((rand()%1) ? true : false));
366  resection->setIsVisible(((rand()%1) ? true : false));
367  ::fwData::Reconstruction::sptr recInput = ::fwData::Reconstruction::New();
368 
370  ::fwData::Resection::ResectionInputs inputs;
371  inputs.push_back(recInput);
372  resection->setInputs(inputs);
373 
374  ::fwData::Reconstruction::sptr recOutput = ::fwData::Reconstruction::New();
376  ::fwData::Resection::ResectionOutputs outputs;
377  outputs.push_back(recOutput);
378  resection->setOutputs(outputs);
379 
380  ::fwData::PlaneList::PlaneListContainer planes;
381  planes.push_back(generatePlane());
382  planes.push_back(generatePlane());
383  ::fwData::PlaneList::sptr planeList = ::fwData::PlaneList::New();
384  planeList->setPlanes(planes);
385 
386  return resection;
387 }
388 
389 //------------------------------------------------------------------------------
390 
391 ::fwData::ResectionDB::sptr Object::generateResectionDB()
392 {
393  ::fwData::ResectionDB::sptr resectionDB = ::fwData::ResectionDB::New();
394  resectionDB->addResection(generateResection());
395  return resectionDB;
396 }
397 
398 //------------------------------------------------------------------------------
399 
400 } // namespace generator
401 } // namespace fwTest
static FWTEST_API::fwData::Material::sptr createMaterial()
Creates a Material with ambient and diffuse color.
static FWTEST_API::fwData::Point::sptr generatePoint()
Creates a Point with random coordinates.
static FWTEST_API::fwData::Composite::sptr createROITraits()
Return a composite containing the ROITraits and a reference on its StructureTraits and mask node (bec...
static FWTEST_API::fwData::Resection::sptr generateResection()
Creates a Resection with 2 planes, 1 input Reconstruction and 1 output Reconstruction.
static FWTEST_API::fwData::Plane::sptr generatePlane()
Creates a Plane with random points.
static FWTEST_API::fwData::Color::sptr randomizeColor()
Creates a color with random values.
static FWTEST_API::fwData::Composite::sptr createReconstructionTraits()
Return a composite containing the ReconstructionTraits and a reference on its StructureTraits and mas...
Definition: Data.hpp:15
static FWTEST_API::fwData::Node::sptr createNode()
Creates a Node with random ports.
static FWDATA_API::fwData::Object::sptr copy(const ::fwData::Object::csptr &source)
return a copy of the source. if source is a null pointer, return a null pointer.
static FWTEST_API::fwData::ProcessObject::sptr createProcessObject()
Creates a ProcessObject with 3 inputs and 1 output.
static FWTEST_API::fwData::ResectionDB::sptr generateResectionDB()
Creates a ResectionDB containing one Resection.
static FWTEST_API::fwData::StructureTraits::sptr createStructureTraits()
Creates a StructureTraits representing the liver.
static FWTEST_API void generateReconstruction(std::shared_ptr< ::fwData::Reconstruction > rec)
Generate a reconstruction.
static FWTEST_API::fwData::Port::sptr createPort()
Creates a Port with random identifier and type.
static FWTEST_API::fwData::TransferFunction::sptr createTFColor()
Creates a TransferFunction with few values.
static FWTEST_API::fwData::StructureTraitsDictionary::sptr createStructureTraitsDictionary()
Creates a StructureTraitsDictionary with many organs.