Skip to content

Commit 7306fab

Browse files
Further texts and dimensions implementation, new methods to align planes, introduced models called Outputs to OCCT, a lot of refactoring
1 parent f6c73bb commit 7306fab

File tree

41 files changed

+959
-173
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+959
-173
lines changed

packages/dev/babylonjs/lib/api/inputs/base-inputs.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export namespace Base {
2020
export type Vector2 = [number, number];
2121
export type Point3 = [number, number, number];
2222
export type Vector3 = [number, number, number];
23+
export type BoundingBox = { min: Base.Point3, max: Base.Point3, center?: Base.Point3, width?: number, height?: number, length?: number };
2324
export type Line2 = { start: Base.Point2, end: Base.Point2 };
2425
export type Line3 = { start: Base.Point3, end: Base.Point3 };
2526
export type Polyline3 = { points: Base.Point3[], isClosed?: boolean };
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
export * from "@bitbybit-dev/core/lib/api/outputs";

packages/dev/base/lib/api/inputs/base-inputs.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export namespace Base {
77
export type Vector2 = [number, number];
88
export type Point3 = [number, number, number];
99
export type Vector3 = [number, number, number];
10+
export type BoundingBox = { min: Base.Point3, max: Base.Point3, center?: Base.Point3, width?: number, height?: number, length?: number };
1011
export type Line2 = { start: Base.Point2, end: Base.Point2 };
1112
export type Line3 = { start: Base.Point3, end: Base.Point3 };
1213
export type Polyline3 = { points: Base.Point3[], isClosed?: boolean, color?: number[] };

packages/dev/base/lib/api/inputs/point-inputs.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,33 @@ export namespace Point {
351351
*/
352352
transformation: Base.TransformMatrixes[];
353353
}
354+
export class ThreePointsNormalDto {
355+
constructor(point1?: Base.Point3, point2?: Base.Point3, point3?: Base.Point3) {
356+
if (point1 !== undefined) { this.point1 = point1; }
357+
if (point2 !== undefined) { this.point2 = point2; }
358+
if (point3 !== undefined) { this.point3 = point3; }
359+
}
360+
/**
361+
* Point 1
362+
* @default undefined
363+
*/
364+
point1: Base.Point3;
365+
/**
366+
* Point 2
367+
* @default undefined
368+
*/
369+
point2: Base.Point3;
370+
/**
371+
* Point 3
372+
* @default undefined
373+
*/
374+
point3: Base.Point3;
375+
/**
376+
* Reverse normal direction
377+
* @default false
378+
*/
379+
reverseNormal = false;
380+
}
354381
export class RemoveConsecutiveDuplicatesDto {
355382
constructor(points?: Base.Point3[], tolerance?: number, checkFirstAndLast?: boolean) {
356383
if (points !== undefined) { this.points = points; }

packages/dev/base/lib/api/inputs/text-inputs.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ export namespace Text {
198198
chars?: VectorCharResultDto[];
199199
}
200200
export class VectorTextDto {
201-
constructor(text?: string, xOffset?: number, yOffset?: number, height?: number, lineSpacing?: number, letterSpacing?: number, align?: Base.horizontalAlignEnum, extrudeOffset?: number) {
201+
constructor(text?: string, xOffset?: number, yOffset?: number, height?: number, lineSpacing?: number, letterSpacing?: number, align?: Base.horizontalAlignEnum, extrudeOffset?: number, centerOnOrigin?: boolean) {
202202
if (text !== undefined) { this.text = text; }
203203
if (xOffset !== undefined) { this.xOffset = xOffset; }
204204
if (yOffset !== undefined) { this.yOffset = yOffset; }
@@ -207,6 +207,7 @@ export namespace Text {
207207
if (letterSpacing !== undefined) { this.letterSpacing = letterSpacing; }
208208
if (align !== undefined) { this.align = align; }
209209
if (extrudeOffset !== undefined) { this.extrudeOffset = extrudeOffset; }
210+
if (centerOnOrigin !== undefined) { this.centerOnOrigin = centerOnOrigin; }
210211
}
211212
/**
212213
* The text
@@ -266,6 +267,11 @@ export namespace Text {
266267
* @step 0.1
267268
*/
268269
extrudeOffset? = 0;
270+
/**
271+
* Will center text on 0, 0, 0
272+
* @default false
273+
*/
274+
centerOnOrigin = false;
269275
}
270276

271277
}

packages/dev/base/lib/api/inputs/vector-inputs.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,30 @@ export namespace Vector {
7272
*/
7373
tolerance = 1e-7;
7474
}
75+
export class VectorsTheSameDto {
76+
constructor(vec1?: number[], vec2?: number[], tolerance?: number) {
77+
if (vec1 !== undefined) { this.vec1 = vec1; }
78+
if (vec2 !== undefined) { this.vec2 = vec2; }
79+
if (tolerance !== undefined) { this.tolerance = tolerance; }
80+
}
81+
/**
82+
* First vector
83+
* @default undefined
84+
*/
85+
vec1: number[];
86+
/**
87+
* Second vector
88+
* @default undefined
89+
*/
90+
vec2: number[];
91+
/**
92+
* Tolerance value
93+
* @default 1e-7
94+
* @minimum 0
95+
* @maximum Infinity
96+
*/
97+
tolerance = 1e-7;
98+
}
7599
export class VectorDto {
76100
constructor(vector?: number[]) {
77101
if (vector !== undefined) { this.vector = vector; }

packages/dev/base/lib/api/services/point.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,45 @@ export class Point {
126126
return this.geometryHelper.transformControlPoints(rotationTransforms, inputs.points);
127127
}
128128

129+
/**
130+
* Gets a bounding box of the points
131+
* @param inputs Points
132+
* @returns Bounding box of points
133+
* @group extract
134+
* @shortname bounding box pts
135+
* @drawable true
136+
*/
137+
boundingBoxOfPoints(inputs: Inputs.Point.PointsDto): Inputs.Base.BoundingBox {
138+
const xVals = [];
139+
const yVals = [];
140+
const zVals = [];
141+
142+
inputs.points.forEach(pt => {
143+
xVals.push(pt[0]);
144+
yVals.push(pt[1]);
145+
zVals.push(pt[2]);
146+
});
147+
148+
const min = [Math.min(...xVals), Math.min(...yVals), Math.min(...zVals)] as Inputs.Base.Point3;
149+
const max = [Math.max(...xVals), Math.max(...yVals), Math.max(...zVals)] as Inputs.Base.Point3;
150+
const center = [
151+
(min[0] + max[0]) / 2,
152+
(min[1] + max[1]) / 2,
153+
(min[2] + max[2]) / 2,
154+
] as Inputs.Base.Point3;
155+
const width = max[0] - min[0];
156+
const height = max[1] - min[1];
157+
const length = max[2] - min[2];
158+
return {
159+
min,
160+
max,
161+
center,
162+
width,
163+
height,
164+
length,
165+
};
166+
}
167+
129168
/**
130169
* Measures the closest distance between a point and a collection of points
131170
* @param inputs Point from which to measure and points to measure the distance against
@@ -365,6 +404,55 @@ export class Point {
365404
return this.geometryHelper.removeConsecutivePointDuplicates(inputs.points, inputs.checkFirstAndLast, inputs.tolerance);
366405
}
367406

407+
/**
408+
* Creates a normal vector from 3 points
409+
* @param inputs Three points and the reverse normal flag
410+
* @returns Normal vector
411+
* @group create
412+
* @shortname normal from 3 points
413+
* @drawable true
414+
*/
415+
normalFromThreePoints(inputs: Inputs.Point.ThreePointsNormalDto): Inputs.Base.Vector3 {
416+
const p1 = inputs.point1;
417+
const p2 = inputs.point2;
418+
const p3 = inputs.point3;
419+
420+
if (!p1 || !p2 || !p3 || p1.length !== 3 || p2.length !== 3 || p3.length !== 3) {
421+
throw new Error("All points must be arrays of 3 numbers [x, y, z]");
422+
}
423+
424+
// Calculate vector A = p2 - p1
425+
const ax = p2[0] - p1[0];
426+
const ay = p2[1] - p1[1];
427+
const az = p2[2] - p1[2];
428+
429+
// Calculate vector B = p3 - p1
430+
const bx = p3[0] - p1[0];
431+
const by = p3[1] - p1[1];
432+
const bz = p3[2] - p1[2];
433+
434+
// Calculate the cross product N = A x B
435+
let nx = (ay * bz) - (az * by);
436+
let ny = (az * bx) - (ax * bz);
437+
let nz = (ax * by) - (ay * bx);
438+
439+
// Check for collinear points (resulting in a zero vector)
440+
// A zero vector indicates the points don't form a unique plane.
441+
// You might want to handle this case depending on your application.
442+
if (nx === 0 && ny === 0 && nz === 0) {
443+
console.warn("Points are collinear or coincident; cannot calculate a unique normal.");
444+
return undefined; // Or return [0, 0, 0] if that's acceptable
445+
}
446+
447+
if (inputs.reverseNormal) {
448+
nx = -nx;
449+
ny = -ny;
450+
nz = -nz;
451+
}
452+
453+
return [nx, ny, nz];
454+
}
455+
368456
private closestPointFromPointData(inputs: Inputs.Point.ClosestPointFromPointsDto): {
369457
index: number, point: Inputs.Base.Point3, distance: number
370458
} {

packages/dev/base/lib/api/services/text.ts

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Point } from "./point";
66
* Contains various text methods.
77
*/
88
export class TextBitByBit {
9-
constructor(private readonly point: Point) {
9+
constructor(private readonly point: Point) {
1010
}
1111

1212
/**
@@ -155,8 +155,9 @@ export class TextBitByBit {
155155

156156
// manage the list of lines
157157
let maxWidth = 0; // keep track of max width for final alignment
158-
let line = { width: 0, height: 0, chars: [] };
159-
let lines = [];
158+
type Line = { width: number, height: number, chars: Inputs.Text.VectorCharResultDto[] };
159+
let line: Line = { width: 0, height: 0, chars: [] };
160+
let lines: Line[] = [];
160161

161162
const pushLine = () => {
162163
maxWidth = Math.max(maxWidth, line.width);
@@ -206,6 +207,41 @@ export class TextBitByBit {
206207
return line;
207208
}
208209
});
210+
211+
if (inputs.centerOnOrigin) {
212+
const pointsFlat: Inputs.Base.Point3[] = [];
213+
214+
// flatten the lines into a single array of points
215+
lines.forEach((line) => {
216+
line.chars.forEach((vchar) => {
217+
vchar.paths.forEach((path) => {
218+
pointsFlat.push(...path);
219+
});
220+
});
221+
});
222+
223+
const bbox = this.point.boundingBoxOfPoints({
224+
points: pointsFlat,
225+
});
226+
227+
lines.forEach((line) => {
228+
line.chars.forEach((vchar) => {
229+
vchar.paths = vchar.paths.map((path) => {
230+
const pts = this.point.translatePoints({
231+
points: path,
232+
translation: [
233+
-bbox.center[0],
234+
-bbox.center[1],
235+
-bbox.center[2],
236+
],
237+
});
238+
return pts;
239+
});
240+
return vchar;
241+
});
242+
});
243+
}
244+
209245
return lines;
210246
}
211247

packages/dev/base/lib/api/services/vector.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ export class Vector {
3535
removeConsecutiveDuplicateVectors(inputs: Inputs.Vector.RemoveConsecutiveDuplicateVectorsDto): number[][] {
3636
return this.geometryHelper.removeConsecutiveVectorDuplicates(inputs.vectors, inputs.checkFirstAndLast, inputs.tolerance);
3737
}
38+
39+
/**
40+
* Checks if two vectors are the same within a given tolerance
41+
* @param inputs Contains two vectors and a tolerance value
42+
* @returns Boolean indicating if vectors are the same
43+
* @group validate
44+
* @shortname vectors the same
45+
* @drawable false
46+
*/
47+
vectorsTheSame(inputs: Inputs.Vector.VectorsTheSameDto): boolean {
48+
return this.geometryHelper.vectorsTheSame(inputs.vec1, inputs.vec2, inputs.tolerance);
49+
}
50+
3851
/**
3952
* Measures the angle between two vectors in degrees
4053
* @param inputs Contains two vectors represented as number arrays

packages/dev/core/lib/api/inputs/base-inputs.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export namespace Base {
1919
export type Vector2 = [number, number];
2020
export type Point3 = [number, number, number];
2121
export type Vector3 = [number, number, number];
22+
export type BoundingBox = { min: Base.Point3, max: Base.Point3, center?: Base.Point3, width?: number, height?: number, length?: number };
2223
export type Line2 = { start: Base.Point2, end: Base.Point2 };
2324
export type Line3 = { start: Base.Point3, end: Base.Point3 };
2425
export type Polyline3 = { points: Base.Point3[], isClosed?: boolean };

0 commit comments

Comments
 (0)