00001
00002
00003
00004
00005
00006 #ifndef AMROC_MULTICPTLEVELSET_H
00007 #define AMROC_MULTICPTLEVELSET_H
00008
00016 #include "MultiStatCPTLevelSet.h"
00017
00018 #define MAXFILES 40
00019
00026 template <class DataType, int dim>
00027 class MultiCPTLevelSet : public MultiStatCPTLevelSet<DataType,dim> {
00028 typedef MultiStatCPTLevelSet<DataType,dim> base;
00029
00030 public:
00031 typedef typename base::grid_fct_type grid_fct_type;
00032 typedef typename base::grid_data_type grid_data_type;
00033 typedef typename cpt::State<dim,DataType> cpt_type;
00034 typedef ads::FixedArray<dim,DataType> point_type;
00035 typedef ads::FixedArray<dim,int> multi_index_type;
00036
00037 MultiCPTLevelSet() : base() {
00038 base::_Stationary = 0;
00039 }
00040
00041 virtual void register_at(ControlDevice& Ctrl, const std::string& prefix) {
00042 base::register_at(Ctrl,prefix);
00043 }
00044 virtual void register_at(ControlDevice& Ctrl) {
00045 register_at(Ctrl, "");
00046 }
00047
00048 virtual void SetPhi(grid_fct_type& phi, const int Time, const int Level, double t) {
00049 point_type* ver = new point_type[num_vertices];
00050 multi_index_type* con = new multi_index_type[num_connections];
00051
00052 int iver=0, icon=0, idbase=0;
00053 for (int i=0; i<nbreps; i++) {
00054 if (nvertices[i]<=0) continue;
00055 point_type* current_vertices=vertices[i];
00056 vertices[i] = ver;
00057 for (register int j=0; j<nvertices[i]; j++, iver++)
00058 ver[iver] = ForwardTransformN(i,current_vertices[j]);
00059 delete [] current_vertices;
00060
00061 multi_index_type* current_connections=connections[i];
00062 connections[i] = con;
00063 for (register int j=0; j<nconnections[i]; j++, icon++) {
00064 con[icon] = current_connections[j];
00065 con[icon] += idbase;
00066 }
00067 delete [] current_connections;
00068
00069 idbase += nvertices[i];
00070
00071 morigin[i] = ForwardTransformN(i,morigin);
00072 }
00073
00074 base::SetBrep(num_vertices, reinterpret_cast<const DataType*>(vertices[0]),
00075 num_connections, reinterpret_cast<const int*>(connections[0]));
00076
00077 base::SetPhi(phi, Time, Level, t);
00078 }
00079
00080 virtual void SetupData(GridHierarchy* gh, const int& ghosts) {
00081 base::SetupData(gh, ghosts);
00082
00083 if (nbreps > MAXFILES) nbreps=MAXFILES;
00084
00085 num_vertices = 0; num_connections = 0;
00086 for (int i=0; i<nbreps; i++) {
00087 std::ifstream brep_file( brep_filename[i].c_str() );
00088 if ( ! brep_file )
00089 std::cerr << "Could not read " << brep_filename[i] << std::endl;
00090
00091 if (brep_filetype[i]==1) {
00092 int fdim, mdim;
00093 brep_file >> fdim >> mdim;
00094 if ( fdim!=dim || mdim!=dim-1 ) {
00095 std::cerr << "Mesh in b-rep file has wrong dimension.\n";
00096 continue;
00097 }
00098 brep_file >> nvertices[i];
00099 vertices[i] = new point_type[ nvertices[i] ];
00100 const point_type* point_iter_end = vertices[i] + nvertices[i];
00101 for ( point_type* piter = vertices[i]; piter != point_iter_end; ++piter )
00102 brep_file >> *piter;
00103 brep_file >> nconnections[i];
00104 connections[i] = new multi_index_type[ nconnections[i] ];
00105 }
00106 else {
00107 brep_file >> nvertices[i] >> nconnections[i];
00108 vertices[i] = new point_type[ nvertices[i] ];
00109 connections[i] = new multi_index_type[ nconnections[i] ];
00110 const point_type* point_iter_end = vertices[i] + nvertices[i];
00111 for ( point_type* piter = vertices[i]; piter != point_iter_end; ++piter )
00112 brep_file >> *piter;
00113 }
00114
00115 const multi_index_type* connections_iter_end = connections[i] + nconnections[i];
00116 for ( multi_index_type* eiter = connections[i]; eiter != connections_iter_end; ++eiter )
00117 brep_file >> *eiter;
00118
00119 assert( brep_file.good() );
00120 brep_file.close();
00121
00122 num_vertices += nvertices[i];
00123 num_connections += nconnections[i];
00124 }
00125
00126 point_type* ver = new point_type[num_vertices];
00127 multi_index_type* con = new multi_index_type[num_connections];
00128
00129 int iver=0, icon=0, idbase=0;
00130 for (int i=0; i<nbreps; i++) {
00131 if (nvertices[i]<=0) continue;
00132 point_type* current_vertices=vertices[i];
00133 vertices[i] = ver;
00134 for (register int j=0; j<nvertices[i]; j++, iver++)
00135 ver[iver] = ForwardTransformN(i,current_vertices[j]);
00136 delete [] current_vertices;
00137
00138 multi_index_type* current_connections=connections[i];
00139 connections[i] = con;
00140 for (register int j=0; j<nconnections[i]; j++, icon++) {
00141 con[icon] = current_connections[j];
00142 con[icon] += idbase;
00143 }
00144 delete [] current_connections;
00145
00146 idbase += nvertices[i];
00147
00148 morigin[i] = ForwardTransformN(i,morigin);
00149 }
00150
00151 base::SetBrep(num_vertices, reinterpret_cast<const DataType*>(vertices[0]),
00152 num_connections, reinterpret_cast<const int*>(connections[0]));
00153
00154 }
00155
00156 point_type ForwardTransformN(const int i, const point_type& p) {
00157 point_type r=p;
00158 r -= origin[i];
00159 point_type a=rotate[i]/180.*pi;
00160 if (dim==3) {
00161 DataType x,y,z,sx,sy,sz,cx,cy,cz;
00162 if (a(0)!=0.) { cx=std::cos(a(0)); sx=std::sin(a(0)); y=r(1); z=r(2); r(1) = cx*y-sx*z; r(2) = sx*y+cx*z; }
00163 if (a(1)!=0.) { cy=std::cos(a(1)); sy=std::sin(a(1)); x=r(0); z=r(2); r(0) = cy*x-sy*z; r(2) = sy*x+cy*z; }
00164 if (a(2)!=0.) { cz=std::cos(a(2)); sz=std::sin(a(2)); x=r(0); y=r(1); r(0) = cz*x-sz*y; r(1) = sz*x+cz*y; }
00165 }
00166 else if (dim==2) {
00167 DataType x,y,sz,cz;
00168 if (a(0)!=0.) { cz=std::cos(a(0)); sz=std::sin(a(0)); x=r(0); y=r(1); r(0) = cz*x-sz*y; r(1) = sz*x+cz*y; }
00169 }
00170 r *= scale[i];
00171 r += translate[i];
00172 return r;
00173 }
00174
00175 point_type InverseTransformN(const int i, const point_type& p) {
00176 point_type r=p;
00177 r -= translate[i];
00178 r /= scale[i];
00179 point_type a=-rotate[i]/180.*pi;
00180 if (dim==3) {
00181 DataType x,y,z,sx,sy,sz,cx,cy,cz;
00182 if (a(0)!=0.) { cx=std::cos(a(0)); sx=std::sin(a(0)); y=r(1); z=r(2); r(1) = cx*y-sx*z; r(2) = sx*y+cx*z; }
00183 if (a(1)!=0.) { cy=std::cos(a(1)); sy=std::sin(a(1)); x=r(0); z=r(2); r(0) = cy*x-sy*z; r(2) = sy*x+cy*z; }
00184 if (a(2)!=0.) { cz=std::cos(a(2)); sz=std::sin(a(2)); x=r(0); y=r(1); r(0) = cz*x-sz*y; r(1) = sz*x+cz*y; }
00185 }
00186 else if (dim==2) {
00187 DataType x,y,sz,cz;
00188 if (a(0)!=0.) { cz=std::cos(a(0)); sz=std::sin(a(0)); x=r(0); y=r(1); r(0) = cz*x-sz*y; r(1) = sz*x+cz*y; }
00189 }
00190 r += origin[i];
00191 return r;
00192 }
00193
00194 inline void SetNBreps(const int _nbreps) { nbreps = _nbreps; }
00195 inline int NBreps() const { return nbreps; }
00196 inline void SetFilename(const int i, const std::string filename) { brep_filename[i] = filename; }
00197 inline std::string Filename(const int i) const { return brep_filename[i]; }
00198
00199 bool GetNthBrep(const int i, int& nver, const DataType*& ver, int& nconn, const int*& conn) {
00200 nver = nvertices[i]; ver = reinterpret_cast<const DataType*>(vertices[i]);
00201 nconn = nconnections[i]; conn = reinterpret_cast<const int*>(connections[i]);
00202 return ((nver>0) && (nconn>0) && (ver!=0) && (conn!=0));
00203 }
00204
00205 private:
00206 DataType pi;
00207
00208 protected:
00209 int nbreps, num_vertices, num_connections;
00210 int nvertices[MAXFILES], nconnections[MAXFILES];
00211 point_type* vertices[MAXFILES];
00212 multi_index_type* connections[MAXFILES];
00213
00214 public:
00215 std::string brep_filename[MAXFILES];
00216 int brep_filetype[MAXFILES];
00217 point_type origin[MAXFILES], morigin[MAXFILES], rotate[MAXFILES], translate[MAXFILES];
00218 DataType scale[MAXFILES];
00219 };
00220
00221
00222 #endif