moab
|
00001 #include "CropTool.hpp" 00002 #include "vtkPlane.h" 00003 #include "vtkCylinder.h" 00004 #include "vtkSphere.h" 00005 #include "vtkTransform.h" 00006 #include "vtkExtractGeometry.h" 00007 #include "vtkImplicitBoolean.h" 00008 #include "vtkMOABUtils.h" 00009 00010 #include "assert.h" 00011 00012 using namespace moab; 00013 00014 CropTool::CropTool() 00015 { 00016 func_types[0] = func_types[1] = func_types[2] = NONE; 00017 funcs[0] = funcs[1] = funcs[2] = NULL; 00018 boolFunction = NULL; 00019 theFunction = NULL; 00020 } 00021 00022 CropTool::~CropTool() 00023 { 00024 vtkMOABUtils::remove_geom_extractors(); 00025 00026 if (NULL != boolFunction) { 00027 // need to remove the geometry extractor from the pipeline, then delete it 00028 vtkMOABUtils::remove_geom_extractors(); 00029 boolFunction->Delete(); 00030 } 00031 } 00032 00033 vtkImplicitFunction *CropTool::get_the_function(const bool) 00034 { 00035 return theFunction; 00036 } 00037 00038 void CropTool::set_the_function(vtkImplicitFunction *this_func) 00039 { 00040 if (NULL != theFunction) { 00041 vtkMOABUtils::remove_geom_extractors(); 00042 theFunction->Delete(); 00043 } 00044 00045 theFunction = this_func; 00046 vtkMOABUtils::add_geom_extractors(theFunction); 00047 } 00048 00049 void CropTool::type_activated(const int num, const int type_num) 00050 { 00051 //vtkImplicitFunction *the_func = NULL; 00052 00053 vtkPlane *plane; 00054 vtkCylinder *cyl; 00055 vtkSphere *sph; 00056 00057 switch (type_num) { 00058 case NONE: 00059 func_types[num] = NONE; 00060 break; 00061 case PLANEX: 00062 plane = vtkPlane::New(); 00063 assert (NULL != plane); 00064 plane->SetNormal(1.0, 0.0, 0.0); 00065 funcs[num] = plane; 00066 //bool_func->AddFunction(plane); 00067 //theFunction = plane; 00068 func_types[num] = PLANEX; 00069 break; 00070 case PLANEY: 00071 plane = vtkPlane::New(); 00072 assert (NULL != plane); 00073 plane->SetNormal(0.0, 1.0, 0.0); 00074 funcs[num] = plane; 00075 //bool_func->AddFunction(plane); 00076 //theFunction = plane; 00077 func_types[num] = PLANEY; 00078 break; 00079 case PLANEZ: 00080 plane = vtkPlane::New(); 00081 assert (NULL != plane); 00082 plane->SetNormal(0.0, 0.0, 1.0); 00083 funcs[num] = plane; 00084 //bool_func->AddFunction(plane); 00085 //theFunction = plane; 00086 func_types[num] = PLANEZ; 00087 break; 00088 case CYLINDER: 00089 cyl = vtkCylinder::New(); 00090 cyl->SetRadius(1.0); 00091 funcs[num] = cyl; 00092 //bool_func->AddFunction(cyl); 00093 func_types[num] = CYLINDER; 00094 break; 00095 case SPHERE: 00096 sph = vtkSphere::New(); 00097 sph->SetRadius(1.0); 00098 funcs[num] = sph; 00099 //bool_func->AddFunction(sph); 00100 func_types[num] = SPHERE; 00101 break; 00102 } 00103 00104 if (funcs[num] == NULL && theFunction != NULL) { 00105 vtkMOABUtils::remove_geom_extractors(); 00106 theFunction->Delete(); 00107 theFunction = NULL; 00108 } 00109 else 00110 set_the_function(funcs[num]); 00111 } 00112 00113 void CropTool::radius_value_changed(const int num, const int new_val) 00114 { 00115 if (num < 0 || num > 2 || funcs[num] == NULL) { 00116 std::cerr << "No function of that type." << std::endl; 00117 return; 00118 } 00119 00120 vtkCylinder *cyl = vtkCylinder::SafeDownCast(funcs[num]); 00121 if (NULL != cyl) { 00122 cyl->SetRadius(((double)new_val)*0.1); 00123 return; 00124 } 00125 00126 vtkSphere *sph = vtkSphere::SafeDownCast(funcs[num]); 00127 if (NULL != sph) { 00128 sph->SetRadius(((double)new_val)*0.1); 00129 return; 00130 } 00131 00132 std::cerr << "Can't set radius on a plane." << std::endl; 00133 } 00134 00135 void CropTool::xyz_value_changed(const int num, const int new_xyz[3]) 00136 { 00137 double dxyz[3] = {(double) new_xyz[0], (double) new_xyz[1], (double) new_xyz[2]}; 00138 dxyz[0] *= 0.1; dxyz[1] *= 0.1; dxyz[2] *= 0.1; 00139 00140 if (func_types[num] >= PLANEX && func_types[num] <= PLANEZ) { 00141 vtkPlane *this_plane = vtkPlane::SafeDownCast(funcs[num]); 00142 assert(NULL != this_plane); 00143 this_plane->SetOrigin(dxyz); 00144 } 00145 else if (func_types[num] == CYLINDER) { 00146 vtkCylinder *this_cyl = vtkCylinder::SafeDownCast(funcs[num]); 00147 assert(NULL != this_cyl); 00148 this_cyl->SetCenter(dxyz); 00149 } 00150 else if (func_types[num] == SPHERE) { 00151 vtkSphere *this_sph = vtkSphere::SafeDownCast(funcs[num]); 00152 assert(NULL != this_sph); 00153 this_sph->SetCenter(dxyz); 00154 } 00155 else { 00156 std::cerr << "No cropping function there." << std::endl; 00157 } 00158 } 00159 00160 void CropTool::rotate_value_changed(const int num, const int new_vals[2]) 00161 { 00162 vtkTransform *transf = get_transform(num); 00163 transf->Identity(); 00164 int val_num = 0; 00165 if (func_types[num] == PLANEY || func_types[num] == PLANEZ || func_types[num] == CYLINDER) 00166 transf->RotateX((double)new_vals[val_num++]); 00167 if (func_types[num] == PLANEX || func_types[num] == PLANEZ || func_types[num] == CYLINDER) 00168 transf->RotateY((double)new_vals[val_num++]); 00169 if (func_types[num] == PLANEX || func_types[num] == PLANEY || func_types[num] == CYLINDER) 00170 transf->RotateZ((double)new_vals[val_num++]); 00171 } 00172 00173 void CropTool::toggle_changed(const int , const bool ) 00174 { 00175 //geomExtractor->SetExtractInside((checked ? 1 : 0)); 00176 } 00177 00178 vtkTransform *CropTool::get_transform(const int num) 00179 { 00180 if (num < 0 || num > 2 || funcs[num] == NULL) { 00181 std::cerr << "No function of that type." << std::endl; 00182 return NULL; 00183 } 00184 00185 // get a transform for this function 00186 vtkAbstractTransform *temp = funcs[num]->GetTransform(); 00187 vtkTransform *transf; 00188 00189 if (NULL == temp) { 00190 transf = vtkTransform::New(); 00191 funcs[num]->SetTransform(transf); 00192 } 00193 else { 00194 transf = vtkTransform::SafeDownCast(temp); 00195 } 00196 00197 return transf; 00198 }