Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit 5c25fc8

Browse files
authored
Merge pull request #263 from Nodi3d/feature/frep-custom-node
Frep custom node feature
2 parents 48cd568 + 22f7d23 commit 5c25fc8

25 files changed

+652
-128
lines changed

core/src/index.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ export * from "./data/NumberTypes";
2020
export * from "./data/PathMask";
2121
export * from "./dom/IndicatorElement";
2222
export * from "./io/Connection";
23-
export * from "./io/IO";
24-
export * from "./io/IOManager";
2523
export * from "./io/Input";
2624
export * from "./io/InputManager";
25+
export * from "./io/IO";
26+
export * from "./io/IOManager";
2727
export * from "./io/Output";
2828
export * from "./io/OutputManager";
2929
export * from "./lib/verb/verb";
@@ -33,6 +33,8 @@ export * from "./math/NMathHelper";
3333
export * from "./math/frep/NFrep";
3434
export * from "./math/frep/NFrepBase";
3535
export * from "./math/frep/NFrepFilter";
36+
export * from "./math/frep/NFrepFunctionFilter";
37+
export * from "./math/frep/NFrepFunctionShape";
3638
export * from "./math/frep/NFrepMatrix";
3739
export * from "./math/frep/NFrepShape";
3840
export * from "./math/frep/blends/NFrepBlend";
@@ -42,6 +44,7 @@ export * from "./math/frep/blends/NFrepSmoothDifferenceBlend";
4244
export * from "./math/frep/blends/NFrepSmoothIntersectionBlend";
4345
export * from "./math/frep/blends/NFrepSmoothUnionBlend";
4446
export * from "./math/frep/blends/NFrepUnionBlend";
47+
export * from "./math/frep/misc/IFrepCustomFunction";
4548
export * from "./math/frep/misc/NFrepMarchingCubes";
4649
export * from "./math/frep/misc/NFrepTexture";
4750
export * from "./math/geometry/FilletCurveHelper";
@@ -80,8 +83,8 @@ export * from "./math/primitive/NDomain";
8083
export * from "./math/primitive/NGroup";
8184
export * from "./misc/Events";
8285
export * from "./misc/ICopyable";
83-
export * from "./misc/IDOMHolder";
8486
export * from "./misc/IDisposable";
87+
export * from "./misc/IDOMHolder";
8588
export * from "./misc/IElementable";
8689
export * from "./misc/INodeSync";
8790
export * from "./misc/ISelectable";
@@ -155,6 +158,9 @@ export * from "./nodes/frep/blend/FrepSmoothDifference";
155158
export * from "./nodes/frep/blend/FrepSmoothIntersection";
156159
export * from "./nodes/frep/blend/FrepSmoothUnion";
157160
export * from "./nodes/frep/blend/FrepUnion";
161+
export * from "./nodes/frep/plugins/FrepCustomBase";
162+
export * from "./nodes/frep/plugins/FrepCustomDistanceFunction";
163+
export * from "./nodes/frep/plugins/FrepCustomFilter";
158164
export * from "./nodes/frep/primitives/FBox";
159165
export * from "./nodes/frep/primitives/FCapsule";
160166
export * from "./nodes/frep/primitives/FCone";
@@ -165,6 +171,7 @@ export * from "./nodes/frep/tpms/TPMSFischerKochS";
165171
export * from "./nodes/frep/tpms/TPMSGyroid";
166172
export * from "./nodes/frep/tpms/TPMSLidinoid";
167173
export * from "./nodes/frep/tpms/TPMSSchwarzP";
174+
export * from "./nodes/frep/utils/FrepBoundingBox";
168175
export * from "./nodes/frep/utils/FrepRound";
169176
export * from "./nodes/frep/utils/MarchingCubes";
170177
export * from "./nodes/importer/CurveImporter";
@@ -301,6 +308,7 @@ export * from "./nodes/surface/freeform/Revolution";
301308
export * from "./nodes/surface/freeform/SurfaceFromPoints";
302309
export * from "./nodes/surface/freeform/Sweep";
303310
export * from "./nodes/surface/primitive/BoundingBox";
311+
export * from "./nodes/surface/primitive/ConstructBoundingBox";
304312
export * from "./nodes/surface/primitive/DeconstructBoundingBox";
305313
export * from "./nodes/surface/primitive/OrientedBoundingBox";
306314
export * from "./nodes/surface/primitive/PlaneSurface";
@@ -370,8 +378,8 @@ export * from "./preview/elements/NVFrep";
370378
export * from "./preview/elements/NVLine";
371379
export * from "./preview/elements/NVMesh";
372380
export * from "./preview/elements/NVPlane";
373-
export * from "./preview/elements/NVPointTransformControls";
374381
export * from "./preview/elements/NVPoints";
382+
export * from "./preview/elements/NVPointTransformControls";
375383
export * from "./preview/elements/NVTexcoordMesh";
376384
export * from "./preview/elements/NVTextSprite";
377385
export * from "./preview/geometries/Axes";

core/src/math/frep/NFrepBase.ts

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ export abstract class NFrepBase implements ITransformable {
1111
return this.compile('p');
1212
}
1313

14+
public toString (): string {
15+
return 'FRep';
16+
}
17+
1418
public abstract applyMatrix (matrix: Matrix4): ITransformable;
1519
public abstract transform (f: TransformerType): ITransformable;
1620
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { NBoundingBox } from '../geometry/NBoundingBox';
2+
import { NFrepBase } from './NFrepBase';
3+
import { NFrepFilter } from './NFrepFilter';
4+
import { IFrepCustomFunction } from './misc/IFrepCustomFunction';
5+
6+
export class NFrepFunctionFilter extends NFrepFilter implements IFrepCustomFunction {
7+
public fn(): string {
8+
return this._fn;
9+
}
10+
11+
protected _fn: string;
12+
13+
constructor (
14+
code: (p: string) => string, frep: NFrepBase, boundingBox: NBoundingBox,
15+
fun: string,
16+
) {
17+
super(code, frep, boundingBox);
18+
this._fn = fun;
19+
}
20+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { NBoundingBox } from '../geometry/NBoundingBox';
2+
import { NFrepShape } from './NFrepShape';
3+
import { IFrepCustomFunction } from './misc/IFrepCustomFunction';
4+
5+
export class NFrepFunctionShape extends NFrepShape implements IFrepCustomFunction {
6+
public fn(): string {
7+
return this._fn;
8+
}
9+
10+
protected _fn: string;
11+
12+
constructor (
13+
code: (p: string) => string, boundingBox: NBoundingBox,
14+
fun: string,
15+
) {
16+
super(code, boundingBox);
17+
this._fn = fun;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
export interface IFrepCustomFunction {
3+
fn(): string
4+
}
5+
6+
const isFrepCustomFunction = function (arg: any): arg is IFrepCustomFunction {
7+
return arg !== null && typeof arg === 'object' && typeof arg.fn === 'function';
8+
};
9+
10+
export {
11+
isFrepCustomFunction
12+
};

core/src/math/frep/misc/NFrepTexture.ts

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { GPUComputationRenderer } from 'three/examples/jsm/misc/GPUComputationRe
33
import { NFrepBase } from '../NFrepBase';
44
import FrepCommon from '../../../shaders/FrepCommon.glsl';
55
import FrepTextureFragment from '../shaders/FrepTexture.frag';
6+
import { isFrepCustomFunction } from './IFrepCustomFunction';
67

78
export type FrepRenderProps = {
89
frep: NFrepBase;
@@ -22,7 +23,9 @@ export class NFrepTexture {
2223

2324
public render (props: FrepRenderProps): WebGLRenderTarget {
2425
const { frep, min, max, width, height, depth } = props;
26+
2527
const code = frep.compile('p');
28+
const custom = isFrepCustomFunction(frep) ? frep.fn() : '';
2629

2730
const tw = width * height;
2831
const th = depth;
@@ -40,6 +43,9 @@ export class NFrepTexture {
4043
FrepTextureFragment.replace(
4144
'#include <frep_common>',
4245
FrepCommon
46+
).replace(
47+
'#include <frep_custom_function>',
48+
custom
4349
),
4450
initialValueTexture
4551
);

core/src/math/frep/shaders/FrepTexture.frag

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ uniform float iU, iV;
66

77
#include <frep_common>
88

9+
// replace to compiled custom function
10+
#include <frep_custom_function>
11+
912
float scene(vec3 p) {
1013
return SCENE_CODE;
1114
}

core/src/math/geometry/NBoundingBox.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ export class NBoundingBox implements IBoundable, ITransformable {
161161
}
162162

163163
public bounds (plane: NPlane): NBoundingBox {
164-
throw new Error('Method not implemented.');
164+
const points = this.projectPoints(plane);
165+
return NBoundingBox.fromPoints(plane, points);
165166
}
166167

167168
public projectPoints (plane: NPlane): NPoint[] {
@@ -238,6 +239,15 @@ export class NBoundingBox implements IBoundable, ITransformable {
238239
];
239240
}
240241

242+
public clone (): NBoundingBox {
243+
return new NBoundingBox(
244+
this.plane.clone(),
245+
this.dx.clone(),
246+
this.dy.clone(),
247+
this.dz.clone()
248+
);
249+
}
250+
241251
public toString (): string {
242252
return `NBoundingBox (x:${this.dx.min}~${this.dx.max}, y:${this.dy.min}~${this.dy.max}, z:${this.dz.min}~${this.dz.max})`;
243253
}

core/src/nodes/NodeDictionary.ts

+12
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ import { FrepSmoothDifference } from './frep/blend/FrepSmoothDifference';
5353
import { FrepSmoothIntersection } from './frep/blend/FrepSmoothIntersection';
5454
import { FrepSmoothUnion } from './frep/blend/FrepSmoothUnion';
5555
import { FrepUnion } from './frep/blend/FrepUnion';
56+
import { FrepCustomDistanceFunction } from './frep/plugins/FrepCustomDistanceFunction';
57+
import { FrepCustomFilter } from './frep/plugins/FrepCustomFilter';
5658
import { FBox } from './frep/primitives/FBox';
5759
import { FCapsule } from './frep/primitives/FCapsule';
5860
import { FCone } from './frep/primitives/FCone';
@@ -63,6 +65,7 @@ import { TPMSFischerKochS } from './frep/tpms/TPMSFischerKochS';
6365
import { TPMSGyroid } from './frep/tpms/TPMSGyroid';
6466
import { TPMSLidinoid } from './frep/tpms/TPMSLidinoid';
6567
import { TPMSSchwarzP } from './frep/tpms/TPMSSchwarzP';
68+
import { FrepBoundingBox } from './frep/utils/FrepBoundingBox';
6669
import { FrepRound } from './frep/utils/FrepRound';
6770
import { MarchingCubes } from './frep/utils/MarchingCubes';
6871
import { CurveImporter } from './importer/CurveImporter';
@@ -194,6 +197,7 @@ import { Revolution } from './surface/freeform/Revolution';
194197
import { SurfaceFromPoints } from './surface/freeform/SurfaceFromPoints';
195198
import { Sweep } from './surface/freeform/Sweep';
196199
import { BoundingBox } from './surface/primitive/BoundingBox';
200+
import { ConstructBoundingBox } from './surface/primitive/ConstructBoundingBox';
197201
import { DeconstructBoundingBox } from './surface/primitive/DeconstructBoundingBox';
198202
import { OrientedBoundingBox } from './surface/primitive/OrientedBoundingBox';
199203
import { PlaneSurface } from './surface/primitive/PlaneSurface';
@@ -307,6 +311,8 @@ const Nodes = {
307311
FrepSmoothIntersection,
308312
FrepSmoothUnion,
309313
FrepUnion,
314+
FrepCustomDistanceFunction,
315+
FrepCustomFilter,
310316
FBox,
311317
FCapsule,
312318
FCone,
@@ -317,6 +323,7 @@ const Nodes = {
317323
TPMSGyroid,
318324
TPMSLidinoid,
319325
TPMSSchwarzP,
326+
FrepBoundingBox,
320327
FrepRound,
321328
MarchingCubes,
322329
CurveImporter,
@@ -448,6 +455,7 @@ const Nodes = {
448455
SurfaceFromPoints,
449456
Sweep,
450457
BoundingBox,
458+
ConstructBoundingBox,
451459
DeconstructBoundingBox,
452460
OrientedBoundingBox,
453461
PlaneSurface,
@@ -563,6 +571,8 @@ const NodeDictionary: { [index: string]: { name:string; entity: NodeConstructorT
563571
'frep/blend/FrepSmoothIntersection': { name: 'FrepSmoothIntersection', entity: FrepSmoothIntersection },
564572
'frep/blend/FrepSmoothUnion': { name: 'FrepSmoothUnion', entity: FrepSmoothUnion },
565573
'frep/blend/FrepUnion': { name: 'FrepUnion', entity: FrepUnion },
574+
'frep/plugins/FrepCustomDistanceFunction': { name: 'FrepCustomDistanceFunction', entity: FrepCustomDistanceFunction },
575+
'frep/plugins/FrepCustomFilter': { name: 'FrepCustomFilter', entity: FrepCustomFilter },
566576
'frep/primitives/FBox': { name: 'FBox', entity: FBox },
567577
'frep/primitives/FCapsule': { name: 'FCapsule', entity: FCapsule },
568578
'frep/primitives/FCone': { name: 'FCone', entity: FCone },
@@ -573,6 +583,7 @@ const NodeDictionary: { [index: string]: { name:string; entity: NodeConstructorT
573583
'frep/tpms/TPMSGyroid': { name: 'TPMSGyroid', entity: TPMSGyroid },
574584
'frep/tpms/TPMSLidinoid': { name: 'TPMSLidinoid', entity: TPMSLidinoid },
575585
'frep/tpms/TPMSSchwarzP': { name: 'TPMSSchwarzP', entity: TPMSSchwarzP },
586+
'frep/utils/FrepBoundingBox': { name: 'FrepBoundingBox', entity: FrepBoundingBox },
576587
'frep/utils/FrepRound': { name: 'FrepRound', entity: FrepRound },
577588
'frep/utils/MarchingCubes': { name: 'MarchingCubes', entity: MarchingCubes },
578589
'importer/CurveImporter': { name: 'CurveImporter', entity: CurveImporter },
@@ -704,6 +715,7 @@ const NodeDictionary: { [index: string]: { name:string; entity: NodeConstructorT
704715
'surface/freeform/SurfaceFromPoints': { name: 'SurfaceFromPoints', entity: SurfaceFromPoints },
705716
'surface/freeform/Sweep': { name: 'Sweep', entity: Sweep },
706717
'surface/primitive/BoundingBox': { name: 'BoundingBox', entity: BoundingBox },
718+
'surface/primitive/ConstructBoundingBox': { name: 'ConstructBoundingBox', entity: ConstructBoundingBox },
707719
'surface/primitive/DeconstructBoundingBox': { name: 'DeconstructBoundingBox', entity: DeconstructBoundingBox },
708720
'surface/primitive/OrientedBoundingBox': { name: 'OrientedBoundingBox', entity: OrientedBoundingBox },
709721
'surface/primitive/PlaneSurface': { name: 'PlaneSurface', entity: PlaneSurface },
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { NFrepFunctionFilter } from '~/src/math/frep/NFrepFunctionFilter';
2+
import { AccessTypes } from '../../../data/AccessTypes';
3+
import { DataAccess } from '../../../data/DataAccess';
4+
import { DataTree } from '../../../data/DataTree';
5+
import { DataTypes } from '../../../data/DataTypes';
6+
import { InputManager } from '../../../io/InputManager';
7+
import { OutputManager } from '../../../io/OutputManager';
8+
import { NFrepBase } from '../../../math/frep/NFrepBase';
9+
import { NFrepFilter } from '../../../math/frep/NFrepFilter';
10+
import { NBoundingBox } from '../../../math/geometry/NBoundingBox';
11+
import { NDomain } from '../../../math/primitive/NDomain';
12+
import { NodeJSONType } from '../../NodeBase';
13+
import { CustomPayloadType } from '../../plugins/Custom';
14+
import { FrepNodeBase } from '../FrepNodeBase';
15+
16+
export type FrepCustomJSONType = NodeJSONType & Partial<CustomPayloadType>;
17+
18+
export abstract class FrepCustomBase extends FrepNodeBase {
19+
public get customName (): string {
20+
return this._customName;
21+
}
22+
23+
public get customProgram (): string {
24+
return this._customProgram;
25+
}
26+
27+
protected abstract _customName: string;
28+
protected abstract _customProgram: string;
29+
30+
public get displayName (): string {
31+
return this._customName;
32+
}
33+
34+
public setupViewElement (container: HTMLDivElement): void {
35+
const span = document.createElement('span');
36+
37+
this.onStateChanged.on(() => {
38+
span.textContent = this.displayName;
39+
});
40+
41+
span.textContent = this.displayName;
42+
container.appendChild(span);
43+
}
44+
45+
public registerInputs (manager: InputManager): void {
46+
manager.add('f', 'Base frep', DataTypes.FREP, AccessTypes.ITEM);
47+
}
48+
49+
public registerOutputs (manager: OutputManager): void {
50+
manager.add('R', 'Frep custom result', DataTypes.FREP, AccessTypes.ITEM);
51+
}
52+
53+
public abstract getCustomSetting (): CustomPayloadType;
54+
55+
public abstract updateCustomSetting (setting: CustomPayloadType): void;
56+
57+
toJSON (name: string): FrepCustomJSONType {
58+
const setting = this.getCustomSetting();
59+
return {
60+
...super.toJSON(name),
61+
...setting
62+
};
63+
}
64+
65+
fromJSON (json: FrepCustomJSONType): void {
66+
const current = this.getCustomSetting();
67+
current.customName = json.customName ?? current.customName;
68+
current.customProgram = json.customProgram ?? current.customProgram;
69+
current.inDataTypes = json.inDataTypes ?? current.inDataTypes;
70+
current.inAccessTypes = json.inAccessTypes ?? current.inAccessTypes;
71+
current.outDataTypes = json.outDataTypes ?? current.outDataTypes;
72+
current.outAccessTypes = json.outAccessTypes ?? current.outAccessTypes;
73+
this.updateCustomSetting(current);
74+
super.fromJSON(json);
75+
}
76+
77+
}

0 commit comments

Comments
 (0)