IOWavefrontOBJ.hpp
1 /*---------------------------------------------------------------------------*\
2  *
3  * mimmo
4  *
5  * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6  *
7  * -------------------------------------------------------------------------
8  * License
9  * This file is part of mimmo.
10  *
11  * mimmo is free software: you can redistribute it and/or modify it
12  * under the terms of the GNU Lesser General Public License v3 (LGPL)
13  * as published by the Free Software Foundation.
14  *
15  * mimmo is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with mimmo. If not, see <http://www.gnu.org/licenses/>.
22  *
23 \*---------------------------------------------------------------------------*/
24 
25 #ifndef __IOWAVEFRONTOBJ__HPP__
26 #define __IOWAVEFRONTOBJ__HPP__
27 
28 #include <BaseManipulation.hpp>
29 
30 namespace mimmo{
31 
46 
47  friend class IOWavefrontOBJ;
48 
49 public:
50 
51  //data
59  //list
60  std::unordered_map<std::string, long> materialsList;
61  std::unordered_map<std::string, long> smoothidsList;
62  std::unordered_map<std::string, long> cellgroupsList;
64  std::unordered_map<long, std::string> inv_materialsList;
65  std::unordered_map<long, std::string> inv_smoothidsList;
66  std::unordered_map<long, std::string> inv_cellgroupsList;
68  // accessory info
69  std::string materialfile;
74  virtual ~WavefrontOBJData(){};
75 
76  void swap(WavefrontOBJData &x) noexcept;
77  void syncListsOnData();
79 
80 protected:
81 
82  void dump(std::ostream & out);
83  void restore(std::istream & out);
84 
85 };
86 
159 
160 public:
166  HARD = 0,
167  SOFT = 1,
169  GETALL = 2,
170  GETALLNOBLANKS = 3
171  };
172 
177  enum class NormalsComputeMode{
178  FLAT_BITPIT = 0,
179  AREA_WEIGHTED = 1,
180  ANGLE_WEIGHTED = 2
181  };
182 
184  virtual ~ManipulateWFOBJData();
185  ManipulateWFOBJData(const bitpit::Config::Section & rootXML);
186 
191 
192  std::vector<long> getPinnedObjectGroup();
193  std::vector<long> getPinnedMaterialGroup();
194  std::vector<long> getPinnedCellGroup();
195  std::vector<long> getPinnedSmoothGroup();
196 
198  void setData(WavefrontOBJData* data);
199  void addAnnotation(MimmoPiercedVector<long>* annotation);
200 
201  void setCheckNormalsMagnitude(bool flag);
204 
205  void setPinObjects(const std::vector<std::string> & objectsList);
206  void setPinMaterials(const std::vector<std::string> & materialsList);
207  void setPinCellGroups(const std::vector<std::string> & cellgroupsList);
208  void setPinSmoothIds(const std::vector<int> & smoothidsList);
209 
210  void clearAnnotations();
212  void clearPinLists();
213  void clear();
214 
215  virtual void absorbSectionXML(const bitpit::Config::Section & slotXML, std::string name = "");
216  virtual void flushSectionXML(bitpit::Config::Section & slotXML, std::string name= "");
217 
218  void execute();
219 
220 protected:
221  void swap(ManipulateWFOBJData & x) noexcept;
222  void buildPorts();
223 
224  void computeAnnotations();
225  void computeNormals();
226  void checkNormalsMagnitude();
227  void extractPinnedLists();
228 
229  std::array<double,3> evalVNormal(bitpit::SurfaceKernel * mesh, long idCell, int locVertex);
230 
233  std::vector<MimmoPiercedVector<long> > m_annotations;
238  std::unordered_set<std::string> m_pinObjects;
239  std::unordered_set<std::string> m_pinMaterials;
240  std::unordered_set<std::string> m_pinCellGroups;
241  std::unordered_set<int> m_pinSmoothIds;
243  std::array<std::vector<long>, 4> m_pinnedCellLists;
245 private:
246  //make useless base class methods private;
247  MimmoSharedPointer<MimmoObject> getGeometry(){return nullptr;};
248  void setGeometry(MimmoSharedPointer<MimmoObject> geo){BITPIT_UNUSED(geo);};
249 };
250 
337 
338 public:
339  typedef std::map<long,std::map<long, std::vector<long>>> TreeGroups;
344  enum class IOMode{
345  READ = 0,
346  RESTORE = 1,
347  WRITE = 2,
348  DUMP = 3
349  };
350 
352  virtual ~IOWavefrontOBJ();
353  IOWavefrontOBJ(const bitpit::Config::Section & rootXML);
354 
355  IOMode whichMode();
356  int whichModeInt();
357 
359  std::unordered_map<long, std::string> getSubParts();
360  std::string getMaterialFile();
361 
365 
368 
370  void setData(WavefrontOBJData* data);
371  void setDir(const std::string & pathdir);
372  void setFilename(const std::string & name);
373  void setGeometryTolerance(double tolerance);
374  void setCleanDoubleMeshVertices(bool clean);
375  void setIgnoreCellGroups(bool ignore);
376  void setTextureUVMode(bool UVmode);
377  void printResumeFile(bool print);
378 
379  virtual void absorbSectionXML(const bitpit::Config::Section & slotXML, std::string name = "");
380  virtual void flushSectionXML(bitpit::Config::Section & slotXML, std::string name= "");
381 
382  void execute();
383 
384 protected:
385  void swap(IOWavefrontOBJ & x) noexcept;
386  void buildPorts();
387 
388  void read(const std::string & fullpath);
389  void write(const std::string & fullpath);
390  void restore(std::istream &);
391  void dump(std::ostream &);
392  void writeResumeFile();
393 
394  std::string searchMaterialFile(std::ifstream & in);
395 
396  void searchObjectPosition(std::ifstream & in,
397  std::vector<std::streampos> & mapPos,
398  std::vector<std::string>& mapNames,
399  std::vector<std::array<long,3>>& mapVCountObject,
400  std::array<long,3>& mapVCountTotal,
401  long &nCellTot);
402 
403  void readObjectData(std::ifstream & in, const std::streampos &begObjectStream, const long &PID,
404  const std::string & defaultGroup, const std::array<long,3> & vCounter,
405  long &vOffset, long &vnOffset, long &vTxtOffset, long &cOffset);
406 
407  void writeObjectData(WavefrontOBJData* objData, std::ofstream & out,
408  const std::array<std::vector<long>,3> & vertexLists,
409  const std::vector<long> & cellList,
410  std::array<long,3> &vOffsets,
411  std::array<std::unordered_map<long,long>,3> & vinsertion_maps,
412  const std::string & defaultGroup,
413  long & activeGroup, long & activeMaterial, long &activeSmoothId);
414 
415  TreeGroups regroupCells(const WavefrontOBJData* objData, const livector1D & cellList);
416 
417 
418 private:
419 
420  IOMode m_mode;
421  std::unique_ptr<WavefrontOBJData> m_intData;
422  WavefrontOBJData * m_extData;
424  std::string m_dir;
425  std::string m_filename;
426  bool m_resume;
427  double m_tol;
428  bool m_cleanDoubleVertices;
429  bool m_ignoringCellGroups;
430  bool m_textureUVMode;
432  //INTERNAL METHODS
433  int convertKeyEntryToInt(char key);
434  long pushCell(MimmoSharedPointer<MimmoObject> geo, std::vector<long> &conn, long PID, long id, int rank = -1 );
435  int checkFacetDefinition(const std::string & str);
436 };
437 
438 REGISTER_PORT(M_GEOM, MC_SCALAR, MD_MIMMO_, __IOWAVEFRONTOBJ__HPP__)
439 REGISTER_PORT(M_WAVEFRONTDATA, MC_SCALAR, MD_WOBJDATA_, __IOWAVEFRONTOBJ__HPP__)
440 REGISTER_PORT(M_STRINGFIELD, MC_SCALAR, MD_MPVECSTRING_, __IOWAVEFRONTOBJ__HPP__)
441 REGISTER_PORT(M_STRINGFIELD2, MC_SCALAR, MD_MPVECSTRING_, __IOWAVEFRONTOBJ__HPP__)
442 REGISTER_PORT(M_LONGFIELD, MC_SCALAR, MD_MPVECLONG_, __IOWAVEFRONTOBJ__HPP__)
443 REGISTER_PORT(M_LONGFIELD2, MC_SCALAR, MD_MPVECLONG_, __IOWAVEFRONTOBJ__HPP__)
444 REGISTER_PORT(M_GEOM2, MC_SCALAR, MD_MIMMO_, __IOWAVEFRONTOBJ__HPP__)
445 REGISTER_PORT(M_GEOM3, MC_SCALAR, MD_MIMMO_, __IOWAVEFRONTOBJ__HPP__)
446 REGISTER_PORT(M_GDISPLS, MC_SCALAR, MD_MPVECARR3FLOAT_, __IOWAVEFRONTOBJ__HPP__)
447 REGISTER_PORT(M_NAME, MC_SCALAR, MD_STRING, __IOWAVEFRONTOBJ__HPP__)
448 REGISTER_PORT(M_VECTORLI, MC_VECTOR, MD_LONG, __IOWAVEFRONTOBJ__HPP__)
449 REGISTER_PORT(M_VECTORLI2, MC_VECTOR, MD_LONG, __IOWAVEFRONTOBJ__HPP__)
450 REGISTER_PORT(M_VECTORLI3, MC_VECTOR, MD_LONG, __IOWAVEFRONTOBJ__HPP__)
451 REGISTER_PORT(M_VECTORLI4, MC_VECTOR, MD_LONG, __IOWAVEFRONTOBJ__HPP__)
452 
453 
454 REGISTER(BaseManipulation, IOWavefrontOBJ, "mimmo.IOWaveFrontOBJ")
455 REGISTER(BaseManipulation, ManipulateWFOBJData, "mimmo.ManipulateWFOBJData")
456 
457 }
458 
459 #endif /* __IOWAVEFRONTOBJ__HPP__ */
void swap(WavefrontOBJData &x) noexcept
virtual void absorbSectionXML(const bitpit::Config::Section &slotXML, std::string name="")
OverlapAnnotationMode getMultipleAnnotationStrategy()
void setRecomputeNormalsCells(MimmoPiercedVector< long > *cellList)
void setGeometryTolerance(double tolerance)
std::unordered_map< long, std::string > inv_materialsList
#define MD_WOBJDATA_
virtual void absorbSectionXML(const bitpit::Config::Section &slotXML, std::string name="")
void swap(ManipulateWFOBJData &x) noexcept
#define M_LONGFIELD
#define M_GDISPLS
MimmoSharedPointer< MimmoObject > textures
void setDir(const std::string &pathdir)
void setTextureUVMode(bool UVmode)
IOMode
Working mode for class IOWavefrontOBJ.
std::string searchMaterialFile(std::ifstream &in)
void read(const std::string &fullpath)
void restore(std::istream &out)
void dump(std::ostream &out)
void setPinCellGroups(const std::vector< std::string > &cellgroupsList)
#define M_GEOM
std::unordered_map< std::string, long > smoothidsList
std::unordered_set< std::string > m_pinObjects
NormalsComputeMode getNormalsComputeStrategy()
std::vector< MimmoPiercedVector< long > > m_annotations
void restore(std::istream &)
std::string getMaterialFile()
MimmoSharedPointer< MimmoObject > getNormals()
Executable block handling io of 3D surface polygonal mesh in *.obj format.
void setGeometry(MimmoSharedPointer< MimmoObject > geo)
MimmoPiercedVector< std::string > materials
std::unordered_map< std::string, long > cellgroupsList
TreeGroups regroupCells(const WavefrontOBJData *objData, const livector1D &cellList)
MimmoPiercedVector< std::string > cellgroups
std::vector< long > livector1D
#define M_VECTORLI3
void setCheckNormalsMagnitude(bool flag)
#define M_GEOM2
WavefrontOBJData * getData()
WavefrontOBJData * getData()
#define M_LONGFIELD2
void searchObjectPosition(std::ifstream &in, std::vector< std::streampos > &mapPos, std::vector< std::string > &mapNames, std::vector< std::array< long, 3 >> &mapVCountObject, std::array< long, 3 > &mapVCountTotal, long &nCellTot)
void readObjectData(std::ifstream &in, const std::streampos &begObjectStream, const long &PID, const std::string &defaultGroup, const std::array< long, 3 > &vCounter, long &vOffset, long &vnOffset, long &vTxtOffset, long &cOffset)
MimmoSharedPointer< MimmoObject > normals
void addAnnotation(MimmoPiercedVector< long > *annotation)
void setMultipleAnnotationStrategy(OverlapAnnotationMode mode)
BaseManipulation is the base class of any manipulation object of the library.
std::unordered_map< long, std::string > getSubParts()
Executable block manipulating optional data of WavefrontOBJ mesh.
#define M_STRINGFIELD2
std::vector< long > getPinnedCellGroup()
void setIgnoreCellGroups(bool ignore)
#define M_STRINGFIELD
#define MC_SCALAR
void setFilename(const std::string &name)
#define MD_MPVECARR3FLOAT_
#define REGISTER_PORT(Name, Container, Datatype, ManipBlock)
std::unordered_map< long, std::string > inv_cellgroupsList
virtual void flushSectionXML(bitpit::Config::Section &slotXML, std::string name="")
MimmoPiercedVector< std::string > * getCellGroups()
void dump(std::ostream &)
#define MD_STRING
#define MD_LONG
MimmoPiercedVector< long > * getSmoothIds()
void setPinSmoothIds(const std::vector< int > &smoothidsList)
void swap(IOWavefrontOBJ &x) noexcept
OverlapAnnotationMode m_annMode
std::unordered_set< std::string > m_pinCellGroups
MimmoPiercedVector< std::string > * getMaterials()
void setData(WavefrontOBJData *data)
#define MC_VECTOR
std::array< double, 3 > evalVNormal(bitpit::SurfaceKernel *mesh, long idCell, int locVertex)
MimmoSharedPointer< MimmoObject > refGeometry
NormalsComputeMode m_normalsMode
#define M_GEOM3
MimmoPiercedVector< long > m_normalsCells
void setNormalsComputeStrategy(NormalsComputeMode mode)
WavefrontOBJData * m_extData
struct for storing cell data attached to Wavefront OBJ polygonal mesh
std::vector< long > getPinnedObjectGroup()
std::unordered_set< std::string > m_pinMaterials
MimmoSharedPointer< MimmoObject > getTextures()
#define M_VECTORLI2
#define MD_MIMMO_
#define M_NAME
NormalsComputeMode
Strategy Mode to recompute Wavefront vertex normals on a set of candidate cells.
std::vector< long > getPinnedMaterialGroup()
void setData(WavefrontOBJData *data)
#define M_WAVEFRONTDATA
void setCleanDoubleMeshVertices(bool clean)
std::unordered_map< long, std::string > inv_smoothidsList
#define M_VECTORLI4
MimmoPiercedVector< long > smoothids
virtual void flushSectionXML(bitpit::Config::Section &slotXML, std::string name="")
void setPinObjects(const std::vector< std::string > &objectsList)
std::unordered_set< int > m_pinSmoothIds
IOWavefrontOBJ(IOMode mode=IOMode::READ)
void write(const std::string &fullpath)
MimmoSharedPointer is a custom implementation of shared pointer.
OverlapAnnotationMode
Strategy Mode to deal with Multiple Annotations on a target cell in ManipulateWFOBJData.
#define MD_MPVECLONG_
void printResumeFile(bool print)
void writeObjectData(WavefrontOBJData *objData, std::ofstream &out, const std::array< std::vector< long >, 3 > &vertexLists, const std::vector< long > &cellList, std::array< long, 3 > &vOffsets, std::array< std::unordered_map< long, long >, 3 > &vinsertion_maps, const std::string &defaultGroup, long &activeGroup, long &activeMaterial, long &activeSmoothId)
#define MD_MPVECSTRING_
std::array< std::vector< long >, 4 > m_pinnedCellLists
#define M_VECTORLI
std::unordered_map< std::string, long > materialsList
#define REGISTER(Base, Derived, name)
Definition: factory.hpp:206
std::vector< long > getPinnedSmoothGroup()
std::map< long, std::map< long, std::vector< long > > > TreeGroups
void setPinMaterials(const std::vector< std::string > &materialsList)