Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(IFCImporter): fix normals #3191

Merged
merged 1 commit into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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