Skip to content
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
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ItemGroup>
<!-- Patch = adds or fixes, minor = breaking while IOScene is 0.x; the -0.0 keeps out previews of the next breaking line
(nuget.org refuses the shorter bound -0 in a dependency). -->
<PackageVersion Include="IOScene" Version="[0.1.0, 0.2.0-0.0)" />
<PackageVersion Include="IOScene" Version="[0.1.2, 0.2.0-0.0)" />
<PackageVersion Include="IOCoord" Version="0.1.0-preview.1" />
<PackageVersion Include="IfcIO.Scene" Version="0.11.1" />
<PackageVersion Include="NwcIO.Scene" Version="0.13.0" />
Expand Down
3 changes: 3 additions & 0 deletions src/IOViewer.Cli/SceneFederation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ internal static void Append(ViewerDocument target, ViewerDocument source, string
line.End = placement.Apply(line.End);
line.Elevation += placement.Translation.Z;
}
foreach (var axisPath in grid.Paths ?? [])
for (int i = 0; i < axisPath.Points.Count; i++)
axisPath.Points[i] = placement.Apply(axisPath.Points[i]);
}
foreach (var schedule in from.Schedules)
{
Expand Down
20 changes: 18 additions & 2 deletions src/IOViewer/Exporters/ViewerPayloadWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ private void ValidateMetadata()
if (!IsFinite(line.Start - _rtcOffset) || !IsFinite(line.End - _rtcOffset))
throw new InvalidDataException($"Grid line '{line.Name}' has a non-finite endpoint.");
}
foreach (var path in _scene.Grids.SelectMany(g => g.Paths ?? []))
{
if (path.Points.Count < 2 || path.Points.Any(point => !IsFinite(point - _rtcOffset)))
throw new InvalidDataException($"Grid path '{path.Name}' needs at least two finite points.");
}
}
if (_options.IncludeClashes)
{
Expand Down Expand Up @@ -898,6 +903,16 @@ private void WriteGrids(Utf8JsonWriter w)
WriteVector(w, "end", line.End - _rtcOffset);
w.WriteEndObject();
}
foreach (var path in _scene.Grids.SelectMany(g => g.Paths ?? []))
{
w.WriteStartObject();
w.WriteString("name", path.Name);
w.WriteStartArray("points");
foreach (var point in path.Points)
WriteVector(w, null, point - _rtcOffset);
w.WriteEndArray();
w.WriteEndObject();
}
}
w.WriteEndArray();
}
Expand Down Expand Up @@ -1048,10 +1063,11 @@ private static void WriteIndices(Utf8JsonWriter w, string name, IEnumerable<int>
w.WriteEndArray();
}

private static void WriteVector(Utf8JsonWriter w, string name, Vector3d v)
private static void WriteVector(Utf8JsonWriter w, string? name, Vector3d v)
{
if (!IsFinite(v)) throw new InvalidDataException($"The '{name}' vector is not finite in viewer coordinates.");
w.WriteStartArray(name);
if (name == null) w.WriteStartArray();
else w.WriteStartArray(name);
w.WriteNumberValue(v.X);
w.WriteNumberValue(v.Y);
w.WriteNumberValue(v.Z);
Expand Down
370 changes: 185 additions & 185 deletions src/IOViewer/Viewer/dist/viewer.js

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion src/IOViewer/Viewer/src/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
const VIEW_TYPES = { f32: Float32Array, u16: Uint16Array, i16: Int16Array, u32: Uint32Array, i32: Int32Array };
const CHUNK_CHARS = 4 * 1024 * 1024; // multiple of 4, so every chunk decodes on its own

export function gridPoints(grid) {
return grid.points ?? [grid.start, grid.end];
}

function block(doc, id) {
const element = doc.getElementById(id);
if (!element) throw new Error(`Data block '${id}' is missing.`);
Expand Down Expand Up @@ -138,7 +142,11 @@ function validateMeta(meta) {
? Number.isFinite(view.camera.orthographicHeight) && view.camera.orthographicHeight > 0
: view.camera.projection === 'perspective' && Number.isFinite(view.camera.fov) && view.camera.fov > 0 && view.camera.fov < 180)
&& count(view.missingComponents) && count(view.ambiguousComponents);
if (meta.grids?.some((grid) => !vector(grid.start) || !vector(grid.end))
const validGrid = (grid) => {
const points = gridPoints(grid);
return Array.isArray(points) && points.length >= 2 && points.every(finiteVector);
};
if (meta.grids?.some((grid) => !validGrid(grid))
|| meta.viewpoints?.some((view) => !validView(view))
|| meta.clashTests?.some((test) => !records(test.clashes) || test.clashes.some((clash) => !vector(clash.point)))) {
throw new ModelDataError('Invalid META grids, viewpoints or clashes.');
Expand Down
8 changes: 5 additions & 3 deletions src/IOViewer/Viewer/src/nav/controls.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as THREE from 'three';
import { gridPoints } from '../data.js';
import { isClick, orbit, zoomTowards, wheelZoomFactor, boxSelectMode, interpolateView, poleCrossing } from './math.js';

const ORBIT_SPEED = 0.005; // radians per pixel
Expand All @@ -16,9 +17,10 @@ export function needsLogarithmicDepth({ bbox, grids }) {
if (bounds.isEmpty()) return true;
const point = new THREE.Vector3();
for (const grid of grids ?? []) {
if (!finitePoint(grid.start) || !finitePoint(grid.end)) return true;
bounds.expandByPoint(point.fromArray(grid.start));
bounds.expandByPoint(point.fromArray(grid.end));
for (const p of gridPoints(grid)) {
if (!finitePoint(p)) return true;
bounds.expandByPoint(point.fromArray(p));
}
}
// Inside the bounding sphere, its diameter bounds far. Keep early depth testing when this fits the linear range.
return bounds.getSize(point).length() * FAR_MARGIN > NEAR_MIN * LINEAR_DEPTH_RATIO;
Expand Down
20 changes: 14 additions & 6 deletions src/IOViewer/Viewer/src/tools/grids.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
import * as THREE from 'three';
import { gridPoints } from '../data.js';
import { el, placeLabel } from '../ui/dom.js';

/**
* Grid axes from META grids ([{name, start, end}] in RTC): thin lines at their own elevation in `group` (an overlay
* Grid axes from META grids (endpoint pairs or paths in RTC): thin lines at their own elevation in `group` (an overlay
* outside ModelScene.root, so never picked), drawn over the model, and a round label with the axis name at both ends
* of each line, placed by update(camera, rect) before each render. Shown from the start when there are any.
* Dispatches 'change' when shown or hidden.
*/
export class GridAxes extends EventTarget {
constructor(root, grids) {
super();
this.count = grids.length;
const positions = [];
this._labels = [];
this.count = 0;
for (const grid of grids) {
const points = gridPoints(grid);
if (points.length < 2) continue;
this.count++;
for (let i = 1; i < points.length; i++) positions.push(...points[i - 1], ...points[i]);
for (const point of [points[0], points[points.length - 1]])
this._labels.push({ element: el('div', 'ioviewer-grid-label', grid.name), point: new THREE.Vector3(...point) });
}
this.group = new THREE.LineSegments(
new THREE.BufferGeometry().setAttribute('position', new THREE.Float32BufferAttribute(grids.flatMap((g) => [...g.start, ...g.end]), 3)),
new THREE.BufferGeometry().setAttribute('position', new THREE.Float32BufferAttribute(positions, 3)),
new THREE.LineBasicMaterial({ color: '#93c5fd', transparent: true, opacity: 0.55, depthTest: false, depthWrite: false }),
);
this.group.name = 'grids';
this.group.geometry.computeBoundingBox();
this.box = this.group.geometry.boundingBox; // all the lines; empty without grids
this.group.renderOrder = 999;
this._labels = grids.flatMap((g) =>
[g.start, g.end].map((p) => ({ element: el('div', 'ioviewer-grid-label', g.name), point: new THREE.Vector3(...p) })),
);
const layer = el('div', 'ioviewer-grid-labels');
layer.append(...this._labels.map((l) => l.element));
root.prepend(layer); // under the panels and the toolbar
Expand Down
8 changes: 8 additions & 0 deletions src/IOViewer/Viewer/test/unit/data.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ const emptyMeta = {
};
const emptyMetaBlock = { textContent: JSON.stringify(emptyMeta) };

test('accepts ordered grid paths and requires at least two finite points', () => {
const read = (grids) => readModelMeta({ getElementById: () => ({ textContent: JSON.stringify({ ...emptyMeta, grids }) }) });
const grids = [{ start: [0, 0, 0], end: [1, 0, 0] }, { points: [[0, 0, 0], [1, 1, 0], [2, 1, 0]] }];
assert.deepEqual(read(grids).grids, grids);
assert.throws(() => read([{ points: [[0, 0, 0]] }]), ModelDataError);
assert.throws(() => read([{ points: [[0, 0, 0], [1, 0, null]] }]), ModelDataError);
});

test('accepts optional source identity and rejects malformed model or level keys', () => {
const read = (meta) => readModelMeta({ getElementById: () => ({ textContent: JSON.stringify(meta) }) });
const meta = { ...emptyMeta, models: [{ id: 0, name: 'Architecture' }], elements: [{ levelId: 4 }, { modelId: 0 }] };
Expand Down
1 change: 1 addition & 0 deletions src/IOViewer/Viewer/test/unit/nav.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ test('logarithmic depth covers a wide model or grids that extend beyond its boun
assert.equal(needsLogarithmicDepth({ bbox, grids: null }), false);
assert.equal(needsLogarithmicDepth({ bbox: { ...bbox, max: [48, 0, 0] } }), true);
assert.equal(needsLogarithmicDepth({ bbox, grids: [{ start: [-2, 0, 0], end: [40, 0, 0] }] }), true);
assert.equal(needsLogarithmicDepth({ bbox, grids: [{ points: [[0, 0, 0], [-2, 0, 0], [40, 0, 0]] }] }), true);
assert.equal(needsLogarithmicDepth({}), true);
});

Expand Down
2 changes: 2 additions & 0 deletions tests/IOViewer.Tests/FederatedCliTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public void Export_MapsSourceIdentityAndReferencesAndPlacesTheAdditionalModel()
{
ModelId = 1,
Lines = [new() { Start = Vector3d.Zero, End = Vector3d.UnitX, Elevation = 1 }],
Paths = [new() { Name = "Arc", Points = [new(0, 0, 0), new(1, 0, 0), new(2, 1, 0)] }],
});
services.Scene.Schedules.Add(new()
{
Expand Down Expand Up @@ -97,6 +98,7 @@ public void Export_MapsSourceIdentityAndReferencesAndPlacesTheAdditionalModel()
Assert.Equal(new Vector3d(10, 22, 4), scene.Instances[2].Transform!.Value.Translation);
Assert.Equal(new Vector3d(10, 24, 4), scene.Instances[2].Transform!.Value.Apply(new(2, 0, 0)));
Assert.Equal(new Vector3d(10, 21, 3), Assert.Single(Assert.Single(scene.Grids).Lines).End);
Assert.Equal([new(10, 20, 3), new(10, 21, 3), new(9, 22, 3)], Assert.Single(scene.Grids[0].Paths!).Points);
Assert.Equal([3L, 4L], scene.Schedules[0].Tasks[0].Children[0].ElementIds);
var clash = scene.ClashTests[0].Clashes[0];
Assert.Equal((3L, 4L), (clash.Element1Id, clash.Element2Id));
Expand Down
5 changes: 5 additions & 0 deletions tests/IOViewer.Tests/HtmlViewerExporterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ private static ViewerDocument GeoreferencedModel()
ModelId = 1,
Name = "Structural Grids",
Lines = [new SceneGridLine { Name = "Axis 1", Start = new(OffsetX, OffsetY, 0), End = new(OffsetX + 10, OffsetY, 0) }],
Paths = [new SceneGridPath { Name = "Axis A", Points = [new(OffsetX, OffsetY, 0), new(OffsetX + 3, OffsetY + 2, 0), new(OffsetX + 7, OffsetY + 3, 0)] }],
});

scene.Schedules.Add(new SceneSchedule
Expand Down Expand Up @@ -159,6 +160,10 @@ public void Meta_HasFormat3AndRtcOffset()
var camera = meta.GetProperty("viewpoints")[0].GetProperty("camera");
Assert.Equal([20, -32.5, 30], Vec(camera.GetProperty("position")));
Assert.Equal([-5, -7.5, 0], Vec(meta.GetProperty("grids")[0].GetProperty("start")));
var path = meta.GetProperty("grids")[1];
Assert.Equal("Axis A", path.GetProperty("name").GetString());
Assert.Equal([-5, -7.5, 0, -2, -5.5, 0, 2, -4.5, 0],
path.GetProperty("points").EnumerateArray().SelectMany(Vec));
Assert.Equal("Pour Slab", meta.GetProperty("timeliner").GetProperty("tasks")[0].GetProperty("name").GetString());
var clash = meta.GetProperty("clashTests")[0].GetProperty("clashes")[0];
Assert.Equal("Slab vs Column", clash.GetProperty("name").GetString());
Expand Down