Skip to content

Commit

Permalink
fix(IFCImporter): fix normals
Browse files Browse the repository at this point in the history
fix #3189
  • Loading branch information
daker committed Dec 15, 2024
1 parent 89758ed commit dacb9b6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 14 deletions.
15 changes: 15 additions & 0 deletions Sources/IO/Geometry/IFCImporter/example/controller.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<table>
<tr>
<td colspan="2">
<h3 style="margin: 5px 0;">Options</h3>
</td>
</tr>
<tr>
<td>
Merge geometries
</td>
<td>
<input type="checkbox" id="mergeGeometries" name="mergeGeometries" value="1" checked>
</td>
</tr>
</table>
38 changes: 30 additions & 8 deletions Sources/IO/Geometry/IFCImporter/example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,30 @@ import '@kitware/vtk.js/Rendering/Profiles/Geometry';
import vtkFullScreenRenderWindow from '@kitware/vtk.js/Rendering/Misc/FullScreenRenderWindow';
import vtkResourceLoader from '@kitware/vtk.js/IO/Core/ResourceLoader';
import vtkIFCImporter from '@kitware/vtk.js/IO/Geometry/IFCImporter';
import vtkURLExtract from '@kitware/vtk.js/Common/Core/URLExtract';

import controlPanel from './controller.html';

// ----------------------------------------------------------------------------
// Example code
// ----------------------------------------------------------------------------

const userParams = vtkURLExtract.extractURLParameters();
const fullScreenRenderer = vtkFullScreenRenderWindow.newInstance();
fullScreenRenderer.addController(controlPanel);

if (userParams.mergeGeometries === undefined) {
userParams.mergeGeometries = true;
}
const mergeGeometriesCheckbox = document.querySelector('#mergeGeometries');
mergeGeometriesCheckbox.checked = Boolean(userParams.mergeGeometries);

const importer = vtkIFCImporter.newInstance({
mergeGeometries: true,
mergeGeometries: mergeGeometriesCheckbox.checked,
});

// ----------------------------------------------------------------------------
function update() {
const fullScreenRenderer = vtkFullScreenRenderWindow.newInstance();
const renderer = fullScreenRenderer.getRenderer();
const renderWindow = fullScreenRenderer.getRenderWindow();

Expand All @@ -40,10 +52,20 @@ vtkResourceLoader
// Pass WebIFC api to vtkIFCImporter
vtkIFCImporter.setIFCAPI(window.WebIFC);

// Trigger data download
importer
.setUrl(
'https://raw.githubusercontent.com/ThatOpen/engine_web-ifc/refs/heads/main/tests/ifcfiles/public/duplex.ifc'
)
.then(update);
if (userParams.fileURL) {
// Trigger data download
importer.setUrl(userParams.fileURL).then(update);
} else {
importer
.setUrl(
'https://raw.githubusercontent.com/ThatOpen/engine_web-ifc/refs/heads/main/tests/ifcfiles/public/duplex.ifc'
)
.then(update);
}
});

document.querySelectorAll("input[name='mergeGeometries']").forEach((input) => {
input.addEventListener('change', (evt) => {
window.location = `?mergeGeometries=${evt.target.checked}`;
});
});
21 changes: 15 additions & 6 deletions Sources/IO/Geometry/IFCImporter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import vtkActor from 'vtk.js/Sources/Rendering/Core/Actor';
import vtkMapper from 'vtk.js/Sources/Rendering/Core/Mapper';
import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray';
import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData';
import vtkPolyDataNormals from 'vtk.js/Sources/Filters/Core/PolyDataNormals';
import vtkCellArray from 'vtk.js/Sources/Common/Core/CellArray';
import vtkAppendPolyData from 'vtk.js/Sources/Filters/General/AppendPolyData';
import vtkMatrixBuilder from 'vtk.js/Sources/Common/Core/MatrixBuilder';
Expand Down Expand Up @@ -244,32 +245,40 @@ function vtkIFCImporter(publicAPI, model) {
publicAPI.importActors = (renderer) => {
if (model.mergeGeometries) {
const opaqueMeshes = meshes.filter((mesh) => mesh.color.w === 1);
let apd = vtkAppendPolyData.newInstance();
const oapd = vtkAppendPolyData.newInstance();

opaqueMeshes.forEach((mesh) => {
const pd = createColoredPolyDataFromIFCMesh(mesh);
apd.addInputData(pd);
oapd.addInputData(pd);
});

const opdn = vtkPolyDataNormals.newInstance();
opdn.setInputConnection(oapd.getOutputPort());
opdn.setComputePointNormals(true);

let mapper = vtkMapper.newInstance();
mapper.setColorModeToDirectScalars();
mapper.setInputConnection(apd.getOutputPort());
mapper.setInputConnection(opdn.getOutputPort());

let actor = vtkActor.newInstance();
actor.setMapper(mapper);
renderer.addActor(actor);

const transparentMeshes = meshes.filter((mesh) => mesh.color.w < 1);
apd = vtkAppendPolyData.newInstance();
const tapd = vtkAppendPolyData.newInstance();

transparentMeshes.forEach((mesh) => {
const pd = createColoredPolyDataFromIFCMesh(mesh);
apd.addInputData(pd);
tapd.addInputData(pd);
});

const tpdn = vtkPolyDataNormals.newInstance();
tpdn.setInputConnection(tapd.getOutputPort());
tpdn.setComputePointNormals(true);

mapper = vtkMapper.newInstance();
mapper.setColorModeToDirectScalars();
mapper.setInputConnection(apd.getOutputPort());
mapper.setInputConnection(tpdn.getOutputPort());

actor = vtkActor.newInstance();
actor.setMapper(mapper);
Expand Down

0 comments on commit dacb9b6

Please sign in to comment.