add dimension to origin

master
howard 2021-04-07 21:05:54 -07:00
parent a7c211a105
commit ea32b0cc73
12 changed files with 112 additions and 60 deletions

BIN
dist/solver.wasm vendored

Binary file not shown.

View File

@ -300,7 +300,7 @@ async function addSketch() {
} }
window.sc = new Scene(store) window.sc = new Scene(store)
sc.loadState() // sc.loadState()

View File

@ -2,11 +2,11 @@
import * as THREE from '../node_modules/three/src/Three'; import * as THREE from '../node_modules/three/src/Three';
import { _vec2, _vec3, raycaster, awaitPts } from './shared' import { _vec2, _vec3, raycaster, awaitPts, ptObj } from './shared'
import { drawOnClick1, drawOnClick2, drawPreClick2, drawClear } from './drawEvents' import { drawOnClick1, drawOnClick2, drawPreClick2, drawClear, drawPoint } from './drawEvents'
import { onHover, onDrag, onPick, onRelease } from './mouseEvents' import { onHover, onDrag, onPick, onRelease } from './mouseEvents'
import { setCoincident } from './constraintEvents' import { setCoincident, setOrdinate } from './constraintEvents'
import { get3PtArc } from './drawArc' import { get3PtArc } from './drawArc'
import { replacer, reviver } from './utils' import { replacer, reviver } from './utils'
import { AxesHelper } from './sketchAxes' import { AxesHelper } from './sketchAxes'
@ -47,13 +47,25 @@ class Sketch {
this.l_id = 0; this.l_id = 0;
this.constraints = new Map() this.constraints = new Map()
this.c_id = 0; this.c_id = 1;
this.obj3d.add(new THREE.Group().add(new AxesHelper(0.5))); this.obj3d.add(new THREE.Group().add(new AxesHelper(0.5)));
this.obj3d.add(new THREE.Group()); this.obj3d.add(new THREE.Group());
this.obj3d.add(new THREE.Group()); this.obj3d.add(new THREE.Group());
this.labels = [] this.labels = []
const p1 = ptObj()
p1.matrixAutoUpdate = false;
p1.userData.constraints = []
this.obj3d.add(p1)
this.updatePointsBuffer()
} else { } else {
@ -199,6 +211,14 @@ class Sketch {
case 'd': case 'd':
this.drawDimension() this.drawDimension()
this.mode = "" this.mode = ""
break;
case 'p':
this.canvas.addEventListener('pointerdown',
(e) => {
drawPoint.call(this, e)
}
)
break; break;
case 'x': case 'x':
this.deleteSelected() this.deleteSelected()
@ -207,6 +227,18 @@ class Sketch {
setCoincident.call(this) setCoincident.call(this)
this.mode = ""
break;
case 'v':
setOrdinate.call(this, 0)
this.mode = ""
break;
case 'h':
setOrdinate.call(this, 1)
this.mode = "" this.mode = ""
break; break;
@ -340,12 +372,12 @@ class Sketch {
updateBoundingSpheres() { updateBoundingSpheres() {
for (let x = 3; x < this.obj3d.children.length; x++) { for (let x = 3; x < this.obj3d.children.length; x++) { // geometry boundign spheres
const obj = this.obj3d.children[x] const obj = this.obj3d.children[x]
obj.geometry.computeBoundingSphere() obj.geometry.computeBoundingSphere()
} }
for (let x = 0; x < this.obj3d.children[1].children.length; x++) { for (let x = 0; x < this.obj3d.children[1].children.length; x++) { // dimension bounding sphere
const obj = this.obj3d.children[1].children[x] const obj = this.obj3d.children[1].children[x]
obj.geometry.computeBoundingSphere() obj.geometry.computeBoundingSphere()
} }

View File

@ -1,22 +1,5 @@
export function addDimension(ent1, ent2, distance) {
// if (ent1.type ==)
this.constraints.set(++this.c_id,
[
'distance', distance,
[p1, p2, -1, -1]
]
)
ent1.userData.constraints.push(this.c_id)
ent2.userData.constraints.push(this.c_id)
}
export function setCoincident() { export function setCoincident() {
const s = new Set() const s = new Set()
const toComb = [] const toComb = []
@ -41,12 +24,37 @@ export function setCoincident() {
this.updateOtherBuffers() this.updateOtherBuffers()
this.solve() this.solve()
this.updateBoundingSpheres()
// update state of points // update state of points
for (let obj of this.selected) { // for (let obj of this.selected) {
obj.geometry.computeBoundingSphere() // obj.geometry.computeBoundingSphere()
obj.material.color.set(0x555555) // obj.material.color.set(0x555555)
} // }
this.selected = [] this.selected = []
this.obj3d.dispatchEvent({ type: 'change' }) this.obj3d.dispatchEvent({ type: 'change' })
} }
export function setOrdinate(dir = 0) {
const line = this.selected[0]
this.constraints.set(++this.c_id,
[
dir ? 'vertical' : 'horizontal', -1,
[-1, -1, line.name, -1] ///////
]
)
line.userData.constraints.push(this.c_id)
this.updateOtherBuffers()
this.solve()
this.updateBoundingSpheres()
this.selected = []
this.obj3d.dispatchEvent({ type: 'change' })
}

View File

@ -2,6 +2,7 @@
import { drawArc, drawArc2 } from './drawArc' import { drawArc, drawArc2 } from './drawArc'
import { drawLine, drawLine2 } from './drawLine' import { drawLine, drawLine2 } from './drawLine'
// import { drawDimension } from "./drawDimension"; // import { drawDimension } from "./drawDimension";
import { ptObj } from './shared'
export function drawOnClick1(e) { export function drawOnClick1(e) {
if (e.buttons !== 1) return if (e.buttons !== 1) return
@ -95,3 +96,16 @@ export function drawClear() {
this.toPush = [] this.toPush = []
} }
} }
export function drawPoint(e) {
const mouseLoc = this.getLocation(e).toArray();
const p1 = ptObj()
p1.matrixAutoUpdate = false;
p1.userData.constraints = []
p1.geometry.attributes.position.set(mouseLoc)
this.obj3d.add(p1)
this.updatePointsBuffer()
this.obj3d.dispatchEvent({ type: 'change' })
}

View File

@ -33,6 +33,8 @@ export function drawLine(mouseLoc) {
p1.userData.constraints.push(this.c_id) p1.userData.constraints.push(this.c_id)
this.obj3d.children[this.obj3d.children.length - 2].userData.constraints.push(this.c_id) this.obj3d.children[this.obj3d.children.length - 2].userData.constraints.push(this.c_id)
} }

View File

@ -64,7 +64,7 @@ export function extrude(sketch) {
} }
findPair(children[3]) //??? need fixing findPair(children[4]) //??? need fixing
const shape = new THREE.Shape(v2s); const shape = new THREE.Shape(v2s);
const extrudeSettings = { depth: 8, bevelEnabled: false }; const extrudeSettings = { depth: 8, bevelEnabled: false };

View File

@ -5,7 +5,10 @@
import { LineSegments, MeshBasicMaterial, Float32BufferAttribute, BufferGeometry, Mesh, DoubleSide } from '../node_modules/three/src/Three' import { LineSegments, MeshBasicMaterial, Float32BufferAttribute, BufferGeometry, Mesh, DoubleSide } from '../node_modules/three/src/Three'
class Patch extends Mesh {
import * as THREE from '../node_modules/three/src/Three';
class Patch extends THREE.Mesh {
constructor(s = 1) { constructor(s = 1) {

View File

@ -12,7 +12,7 @@ export const ToolTip = () => {
* child mouseover is novel. If it's not, we ignore the event * child mouseover is novel. If it's not, we ignore the event
*/ */
const [state, setState] = useState(null) const [text, setText] = useState(null)
const ref = useRef() const ref = useRef()
@ -38,14 +38,11 @@ export const ToolTip = () => {
prevTooltip.current = tooltip // svg workaround prevTooltip.current = tooltip // svg workaround
clearTimeout(timeout.current) clearTimeout(timeout.current)
if (tooltip) { if (tooltip) {
let { left, top, width, height } = node.getBoundingClientRect() let { left, top, width, height } = node.getBoundingClientRect()
left = left + width / 2 - getTextWidth(tooltip) / 2 - 4 // 4 is padding left = left + width / 2 - getTextWidth(tooltip) / 2 - 4 // 4 is padding
top = top + height + 6 // 6 is arrow height/width top = top + height + 6 // 6 is arrow height/width
setText(tooltip)
setState(tooltip)
if (activated.current) { if (activated.current) {
ref.current.setAttribute('style', `left:${left}px; top:${top}px; visibility:visible`) ref.current.setAttribute('style', `left:${left}px; top:${top}px; visibility:visible`)
} else { } else {
@ -54,22 +51,16 @@ export const ToolTip = () => {
activated.current = true activated.current = true
}, 1000); }, 1000);
} }
} else { } else {
ref.current.setAttribute('style', `visibility:hidden`) ref.current.setAttribute('style', `visibility:hidden`)
activated.current = false activated.current = false
} }
}) })
}, []) }, [])
return <div className="tooltip" ref={ref}> return <div className="tooltip" ref={ref}>
{state} {text}
<div className="arrow"></div> <div className="arrow"></div>
</div> </div>
@ -77,6 +68,7 @@ export const ToolTip = () => {
function getTextWidth(text, font = "16px sans-serif") { function getTextWidth(text, font = "16px sans-serif") {
// https://stackoverflow.com/a/21015393
// re-use canvas object for better performance // re-use canvas object for better performance
let canvas = getTextWidth.canvas || (getTextWidth.canvas = document.createElement("canvas")); let canvas = getTextWidth.canvas || (getTextWidth.canvas = document.createElement("canvas"));
let context = canvas.getContext("2d"); let context = canvas.getContext("2d");

View File

@ -7,8 +7,8 @@ const _vec3 = new THREE.Vector3()
const raycaster = new THREE.Raycaster(); const raycaster = new THREE.Raycaster();
raycaster.params.Line.threshold = 0.8; raycaster.params.Line.threshold = 0.1;
raycaster.params.Points.threshold = 0.6; raycaster.params.Points.threshold = 0.1;
const color = { const color = {

View File

@ -11,7 +11,7 @@ class AxesHelper extends LineSegments {
const vertices = [ const vertices = [
0, 0, 0, 0.5 * s, 0, 0, 0, 0, 0, 0.5 * s, 0, 0,
0.5 * s, 0, 0, 0.35 * s, 0.08*s, 0, 0.5 * s, 0, 0, 0.3 * s, 0.08*s, 0,
0, 0, 0, 0, s, 0, 0, 0, 0, 0, s, 0,
]; ];
@ -20,7 +20,7 @@ class AxesHelper extends LineSegments {
const geometry = new BufferGeometry(); const geometry = new BufferGeometry();
geometry.setAttribute('position', new Float32BufferAttribute(vertices, 3)); geometry.setAttribute('position', new Float32BufferAttribute(vertices, 3));
const material = new LineBasicMaterial({ color: 0xff0000, toneMapped: false }); const material = new LineBasicMaterial({ color: 0xff0000, toneMapped: false, linewidth:2 });
super(geometry, material); super(geometry, material);

View File

@ -69,18 +69,16 @@ int solver(int nPts, float *p_ptr, int nConst, float *c_ptr, int nLinks, float *
Slvs_hParam vh = 301, vh_s = 301; Slvs_hParam vh = 301, vh_s = 301;
Slvs_hParam lh = 400, lh_s = 400; Slvs_hParam lh = 400, lh_s = 400;
Slvs_hParam con_id = 1;
float *buf_pt_start = p_ptr; float *buf_pt_start = p_ptr;
int p_start = sys.params; int p_start = sys.params;
for (int i = 0; i < nPts; i++) for (int i = 0; i < nPts; i++)
{ {
if (isnan((float)*p_ptr)) if (isnan((float)*p_ptr))
{ {
// printf("%i\n",i);
p_ptr += 3; p_ptr += 3;
continue; continue;
} }
sys.param[sys.params++] = Slvs_MakeParam(ph++, g, (float)*p_ptr++); sys.param[sys.params++] = Slvs_MakeParam(ph++, g, (float)*p_ptr++);
sys.param[sys.params++] = Slvs_MakeParam(ph++, g, (float)*p_ptr++); sys.param[sys.params++] = Slvs_MakeParam(ph++, g, (float)*p_ptr++);
sys.entity[sys.entities++] = Slvs_MakePoint2d(i, g, 200, ph - 1, ph - 2); sys.entity[sys.entities++] = Slvs_MakePoint2d(i, g, 200, ph - 1, ph - 2);
@ -93,13 +91,10 @@ int solver(int nPts, float *p_ptr, int nConst, float *c_ptr, int nLinks, float *
switch ((int)*l_ptr++) switch ((int)*l_ptr++)
{ {
case 0: case 0:
// printf("matching %i\n",(int)*(l_ptr + 2));
sys.entity[sys.entities++] = Slvs_MakeLineSegment((int)*(l_ptr + 2), g, sys.entity[sys.entities++] = Slvs_MakeLineSegment((int)*(l_ptr + 2), g,
200, (int)*l_ptr, (int)*(l_ptr + 1)); 200, (int)*l_ptr, (int)*(l_ptr + 1));
break; break;
case 1: case 1:
/* And arc, centered at point 303, starting at point 304, ending at
* point 305. */
sys.entity[sys.entities++] = Slvs_MakeArcOfCircle((int)*(l_ptr + 3), g, 200, 102, sys.entity[sys.entities++] = Slvs_MakeArcOfCircle((int)*(l_ptr + 3), g, 200, 102,
(int)*(l_ptr + 2), (int)*(l_ptr), (int)*(l_ptr + 1)); (int)*(l_ptr + 2), (int)*(l_ptr), (int)*(l_ptr + 1));
break; break;
@ -110,11 +105,17 @@ int solver(int nPts, float *p_ptr, int nConst, float *c_ptr, int nLinks, float *
l_ptr += 4; l_ptr += 4;
} }
for (int i = 0; i < nConst; i++, c_ptr += 6) int c_id = 1;
{
// printf("%i here %i\n",(int)*c_ptr, nConst);
sys.constraint[sys.constraints++] = Slvs_MakeConstraint( sys.constraint[sys.constraints++] = Slvs_MakeConstraint(
con_id++, g, c_id++, 2,
SLVS_C_POINTS_COINCIDENT,
200,
-1,
101, 3, -1, -1);
for (; c_id < 2 + nConst; c_id++, c_ptr += 6)
{
sys.constraint[sys.constraints++] = Slvs_MakeConstraint(
c_id, g,
(int)*c_ptr + 100000, (int)*c_ptr + 100000,
200, 200,
*(c_ptr + 1), *(c_ptr + 1),