Skip to content
/ vtk-js Public
forked from Kitware/vtk-js

Commit

Permalink
fix(IFCImporter): fix normals
Browse files Browse the repository at this point in the history
  • Loading branch information
daker committed Dec 18, 2024
1 parent 89758ed commit ff1cf8c
Show file tree
Hide file tree
Showing 3 changed files with 50 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>
29 changes: 22 additions & 7 deletions Sources/IO/Geometry/IFCImporter/example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,33 @@ 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 url =
userParams.fileURL ||
'https://raw.githubusercontent.com/ThatOpen/engine_web-ifc/refs/heads/main/tests/ifcfiles/public/duplex.ifc';
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 @@ -41,9 +56,9 @@ vtkResourceLoader
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);
importer.setUrl(url).then(update);
});

mergeGeometriesCheckbox.addEventListener('change', (evt) => {
window.location = `?mergeGeometries=${evt.target.checked}`;
});
20 changes: 13 additions & 7 deletions Sources/IO/Geometry/IFCImporter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData';
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';
import { mat3 } from 'gl-matrix';

const { vtkErrorMacro } = macro;

Expand Down Expand Up @@ -94,11 +95,16 @@ function vtkIFCImporter(publicAPI, model) {
.buildFromRadian()
.setMatrix(userMatrix);

const normalMatrix = vtkMatrixBuilder
.buildFromRadian()
.multiply3x3(mat3.fromMat4(mat3.create(), userMatrix));

for (let i = 0; i < vertices.length; i += 6) {
const point = [vertices[i], vertices[i + 1], vertices[i + 2]];
const normal = [vertices[i + 3], vertices[i + 4], vertices[i + 5]];

transformMatrix.apply(point).apply(normal);
transformMatrix.apply(point);
normalMatrix.apply(normal);

pointValues[i / 2] = point[0];
pointValues[i / 2 + 1] = point[1];
Expand Down Expand Up @@ -244,32 +250,32 @@ 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);
});

let mapper = vtkMapper.newInstance();
mapper.setColorModeToDirectScalars();
mapper.setInputConnection(apd.getOutputPort());
mapper.setInputConnection(oapd.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);
});

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

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

0 comments on commit ff1cf8c

Please sign in to comment.