[162] | 1 | #include "X3dExporter.h"
|
---|
[712] | 2 | #include "VrmlExporter.h"
|
---|
[1233] | 3 | #include "OspTree.h"
|
---|
[1121] | 4 | #include "KdTree.h"
|
---|
[1315] | 5 | #include "IntersectableWrapper.h"
|
---|
[1287] | 6 | #include "BvHierarchy.h"
|
---|
[1344] | 7 | #include "Triangle3.h"
|
---|
| 8 | #include "Polygon3.h"
|
---|
[162] | 9 |
|
---|
[1121] | 10 |
|
---|
[863] | 11 | namespace GtpVisibilityPreprocessor {
|
---|
[860] | 12 |
|
---|
[162] | 13 | Exporter *
|
---|
| 14 | Exporter::GetExporter(const string filename)
|
---|
| 15 | {
|
---|
[712] | 16 | Exporter *exporter = NULL;
|
---|
[162] | 17 |
|
---|
[712] | 18 | if (strstr(filename.c_str(), ".x3d"))
|
---|
| 19 | {
|
---|
| 20 | exporter = new X3dExporter(filename);
|
---|
| 21 | }
|
---|
| 22 | else if (strstr(filename.c_str(), ".wrl"))
|
---|
| 23 | {
|
---|
| 24 | exporter = new VrmlExporter(filename);
|
---|
[162] | 25 | }
|
---|
[712] | 26 | else
|
---|
| 27 | {
|
---|
| 28 | cerr<<"Error: Currently unsuported export format, filename " << filename << endl;
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | return exporter;
|
---|
[162] | 32 | }
|
---|
| 33 |
|
---|
[1121] | 34 |
|
---|
[1415] | 35 | bool Exporter::ExportOspTree(const OspTree &ospTree,
|
---|
| 36 | const int maxPvs
|
---|
| 37 | )
|
---|
[1121] | 38 | {
|
---|
| 39 | vector<KdLeaf *> leaves;
|
---|
| 40 | ospTree.CollectLeaves(leaves);
|
---|
| 41 | mUseForcedMaterial = true;
|
---|
| 42 |
|
---|
| 43 | vector<KdLeaf *>::const_iterator it, it_end = leaves.end();
|
---|
| 44 |
|
---|
| 45 | Material white;
|
---|
| 46 | white.mDiffuseColor.r = 1;
|
---|
| 47 | white.mDiffuseColor.g = 1;
|
---|
| 48 | white.mDiffuseColor.b = 1;
|
---|
| 49 |
|
---|
| 50 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
| 51 | {
|
---|
| 52 | KdLeaf *leaf = *it;
|
---|
| 53 |
|
---|
| 54 | SetWireframe();
|
---|
| 55 | SetForcedMaterial(white);
|
---|
[1144] | 56 | ExportBox(ospTree.GetBoundingBox(leaf));
|
---|
[1121] | 57 |
|
---|
| 58 | SetFilled();
|
---|
| 59 |
|
---|
| 60 | if (maxPvs) // color code pvs
|
---|
| 61 | {
|
---|
| 62 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
[1181] | 63 | const float importance = (float)leaf->mObjects.size() / (float)maxPvs;
|
---|
[1121] | 64 |
|
---|
| 65 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
| 66 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
| 67 | }
|
---|
| 68 | else
|
---|
| 69 | {
|
---|
| 70 | SetForcedMaterial(RandomMaterial());
|
---|
| 71 | }
|
---|
[1287] | 72 | if (0) ExportGeometry(leaf->mObjects);
|
---|
[1121] | 73 | }
|
---|
| 74 |
|
---|
| 75 | return true;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[1181] | 78 |
|
---|
[1139] | 79 | void Exporter::ExportKdIntersectable(const KdIntersectable &kdObj)
|
---|
| 80 | {
|
---|
[1233] | 81 | KdNode *node = kdObj.GetItem();
|
---|
[1139] | 82 | Intersectable::NewMail();
|
---|
| 83 |
|
---|
[1144] | 84 | // todo: traverse to leaves
|
---|
[1139] | 85 | if (node->IsLeaf())
|
---|
| 86 | {
|
---|
[1144] | 87 | // eyport leaf pvs
|
---|
[1139] | 88 | KdLeaf *leaf = dynamic_cast<KdLeaf *>(node);
|
---|
[1144] | 89 |
|
---|
[1139] | 90 | ObjectContainer::const_iterator oit, oit_end = leaf->mObjects.end();
|
---|
| 91 |
|
---|
| 92 | for (oit = leaf->mObjects.begin(); oit != oit_end; ++ oit)
|
---|
| 93 | {
|
---|
| 94 | Intersectable *obj = *oit;
|
---|
| 95 |
|
---|
| 96 | if (!obj->Mailed())
|
---|
| 97 | {
|
---|
| 98 | ExportIntersectable(obj);
|
---|
| 99 | obj->Mail();
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 |
|
---|
[1287] | 106 | bool Exporter::ExportBvHierarchy(const BvHierarchy &bvHierarchy,
|
---|
[1920] | 107 | const float maxPvs,
|
---|
[1764] | 108 | AxisAlignedBox3 *box,
|
---|
[1418] | 109 | const bool exportBoundingBoxes)
|
---|
[1287] | 110 | {
|
---|
| 111 | vector<BvhLeaf *> leaves;
|
---|
[1707] | 112 | bvHierarchy.CollectLeaves(bvHierarchy.GetRoot(), leaves);
|
---|
[1287] | 113 |
|
---|
| 114 | mUseForcedMaterial = true;
|
---|
| 115 | vector<BvhLeaf *>::const_iterator it, it_end = leaves.end();
|
---|
| 116 |
|
---|
| 117 | Material white;
|
---|
| 118 | white.mDiffuseColor.r = 1;
|
---|
| 119 | white.mDiffuseColor.g = 1;
|
---|
| 120 | white.mDiffuseColor.b = 1;
|
---|
[1293] | 121 |
|
---|
[1287] | 122 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
| 123 | {
|
---|
| 124 | BvhLeaf *leaf = *it;
|
---|
| 125 |
|
---|
[1418] | 126 | if (leaf->mObjects.empty() ||
|
---|
| 127 | (box && !Overlap(*box, leaf->GetBoundingBox())))
|
---|
[1415] | 128 | continue;
|
---|
| 129 |
|
---|
[1418] | 130 | if (exportBoundingBoxes)
|
---|
| 131 | {
|
---|
| 132 | SetWireframe();
|
---|
| 133 | SetForcedMaterial(white);
|
---|
| 134 | ExportBox(leaf->GetBoundingBox());
|
---|
| 135 | }
|
---|
[1287] | 136 |
|
---|
[1920] | 137 | if (maxPvs > 0) // color code pvs
|
---|
[1287] | 138 | {
|
---|
| 139 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
[1920] | 140 | const float importance = (float)leaf->mRenderCost / (float)maxPvs;
|
---|
[1287] | 141 |
|
---|
| 142 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
| 143 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
| 144 | }
|
---|
| 145 | else
|
---|
| 146 | {
|
---|
| 147 | SetForcedMaterial(RandomMaterial());
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[1418] | 150 | SetFilled();
|
---|
[2003] | 151 |
|
---|
| 152 | if (0)
|
---|
| 153 | ExportGeometry(leaf->mObjects, true, box);
|
---|
| 154 | else
|
---|
| 155 | ExportBox(leaf->GetBoundingBox());
|
---|
[1287] | 156 | }
|
---|
| 157 |
|
---|
[1420] | 158 | // reset to filled
|
---|
[1418] | 159 | SetFilled();
|
---|
| 160 |
|
---|
[1287] | 161 | return true;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 |
|
---|
[1344] | 165 | void Exporter::ExportIntersectable(Intersectable *object)
|
---|
| 166 | {
|
---|
| 167 | switch (object->Type())
|
---|
| 168 | {
|
---|
| 169 | case Intersectable::MESH_INSTANCE:
|
---|
| 170 | ExportMeshInstance((MeshInstance *)object);
|
---|
| 171 | break;
|
---|
| 172 | case Intersectable::TRANSFORMED_MESH_INSTANCE:
|
---|
| 173 | ExportTransformedMeshInstance(dynamic_cast<TransformedMeshInstance *>(object));
|
---|
| 174 | break;
|
---|
| 175 | case Intersectable::VIEW_CELL:
|
---|
| 176 | ExportViewCell(dynamic_cast<ViewCell *>(object));
|
---|
| 177 | break;
|
---|
| 178 | case Intersectable::KD_INTERSECTABLE:
|
---|
| 179 | ExportKdIntersectable(*(dynamic_cast<KdIntersectable *>(object)));
|
---|
| 180 | break;
|
---|
| 181 | case Intersectable::TRIANGLE_INTERSECTABLE:
|
---|
| 182 | {
|
---|
[1764] | 183 | // if (mClampToBox && !Overlap(mBoundingBox, object->GetBox())) return;
|
---|
| 184 | const Triangle3 triangle = dynamic_cast<TriangleIntersectable *>(object)->GetItem();
|
---|
| 185 |
|
---|
| 186 | Polygon3 poly(triangle);
|
---|
| 187 | ExportPolygon(&poly);
|
---|
| 188 | break;
|
---|
[1344] | 189 | }
|
---|
[1418] | 190 | case Intersectable::BVH_INTERSECTABLE:
|
---|
| 191 | {
|
---|
[1758] | 192 | BvhNode *node = dynamic_cast<BvhNode *>(object);
|
---|
[1764] | 193 |
|
---|
[1418] | 194 | if (node->IsLeaf())
|
---|
[1764] | 195 | {
|
---|
[1418] | 196 | ExportGeometry(dynamic_cast<BvhLeaf *>(node)->mObjects, true);
|
---|
[1764] | 197 | }
|
---|
| 198 |
|
---|
[1418] | 199 | break;
|
---|
| 200 | }
|
---|
[1344] | 201 | default:
|
---|
| 202 | cerr << "Sorry the export for object type " << Intersectable::GetTypeName(object) << " is not available yet" << endl;
|
---|
| 203 | break;
|
---|
| 204 | }
|
---|
| 205 | }
|
---|
| 206 |
|
---|
[1418] | 207 |
|
---|
[1344] | 208 | void Exporter::ExportMeshInstance(MeshInstance *object)
|
---|
| 209 | {
|
---|
| 210 | // $$JB$$
|
---|
| 211 | // in the future check whether the mesh was not already exported
|
---|
| 212 | // and use a reference to the that mesh instead
|
---|
| 213 | ExportMesh(object->GetMesh());
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 |
|
---|
| 217 | void Exporter::ExportTransformedMeshInstance(TransformedMeshInstance *mi)
|
---|
| 218 | {
|
---|
| 219 | Mesh mesh(*mi->GetMesh());
|
---|
| 220 |
|
---|
| 221 | Matrix4x4 m;
|
---|
| 222 | mi->GetWorldTransform(m);
|
---|
| 223 | mesh.ApplyTransformation(m);
|
---|
| 224 |
|
---|
| 225 | ExportMesh(&mesh);
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 |
|
---|
| 229 | void Exporter::ExportViewCells(const ViewCellContainer &viewCells)
|
---|
| 230 | {
|
---|
| 231 | ViewCellContainer::const_iterator it, it_end = viewCells.end();
|
---|
| 232 |
|
---|
| 233 | for (it = viewCells.begin(); it != it_end; ++ it)
|
---|
| 234 | {
|
---|
| 235 | ExportViewCell(*it);
|
---|
| 236 | }
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 |
|
---|
[1404] | 240 | void Exporter::ExportGeometry(const ObjectContainer &objects,
|
---|
[1415] | 241 | const bool exportSingleMesh,
|
---|
| 242 | AxisAlignedBox3 *bbox)
|
---|
[1404] | 243 | {
|
---|
| 244 | ObjectContainer::const_iterator oit, oit_end = objects.end();
|
---|
[1344] | 245 |
|
---|
[1723] | 246 | if (!exportSingleMesh)
|
---|
[1404] | 247 | {
|
---|
| 248 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
| 249 | {
|
---|
[1418] | 250 | if (!bbox || Overlap(*bbox, (*oit)->GetBox()))
|
---|
[1415] | 251 | {
|
---|
| 252 | if (0) SetForcedMaterial(RandomMaterial());
|
---|
| 253 | ExportIntersectable(*oit);
|
---|
| 254 | }
|
---|
[1404] | 255 | }
|
---|
| 256 | return;
|
---|
| 257 | }
|
---|
[1418] | 258 |
|
---|
| 259 | ///////////////////////
|
---|
[1404] | 260 | //-- all objects exported as one mesh
|
---|
[1418] | 261 | //-- warning: currently works only for triangles
|
---|
[1415] | 262 |
|
---|
[1404] | 263 | PolygonContainer polys;
|
---|
| 264 |
|
---|
| 265 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
| 266 | {
|
---|
| 267 | Intersectable *obj = *oit;
|
---|
[1415] | 268 |
|
---|
[1418] | 269 | if (bbox && !Overlap(*bbox, obj->GetBox()))
|
---|
| 270 | {
|
---|
[1415] | 271 | continue;
|
---|
[1418] | 272 | }
|
---|
[1404] | 273 | switch (obj->Type())
|
---|
| 274 | {
|
---|
| 275 | case Intersectable::TRIANGLE_INTERSECTABLE:
|
---|
[1421] | 276 | {
|
---|
[1404] | 277 | TriangleIntersectable *ti = dynamic_cast<TriangleIntersectable *>(obj);
|
---|
| 278 | polys.push_back(new Polygon3(ti->GetItem()));
|
---|
| 279 | break;
|
---|
| 280 | }
|
---|
[1418] | 281 | case Intersectable::MESH_INSTANCE:
|
---|
[1421] | 282 | {
|
---|
[1418] | 283 | MeshInstance *mi = dynamic_cast<MeshInstance *>(obj);
|
---|
| 284 | ExportMesh(mi->GetMesh());
|
---|
[1421] | 285 | break;
|
---|
[1418] | 286 | }
|
---|
[1404] | 287 | default:
|
---|
| 288 | cout << "merging of object type " << obj->Type() << " not implemented yet" << endl;
|
---|
| 289 | break;
|
---|
| 290 | }
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | Mesh dummyMesh;
|
---|
| 294 | PolygonContainer::const_iterator pit, pit_end = polys.end();
|
---|
| 295 |
|
---|
| 296 | for (pit = polys.begin(); pit != pit_end; ++ pit)
|
---|
| 297 | {
|
---|
| 298 | Polygon3 *poly = (*pit);
|
---|
| 299 | IncludePolyInMesh(*poly, dummyMesh);
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | ExportMesh(&dummyMesh);
|
---|
| 303 | CLEAR_CONTAINER(polys);
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 |
|
---|
| 307 |
|
---|
[1694] | 308 | }
|
---|