Need help to crack an algorithm

I am trying to find points for the curve between two points

These are my two points in x,z planes

startPoint = new THREE.Vector3(1042.3745025363808, 0, -3017.6896180088434)

endPoint = new THREE.Vector3(1042.3575319644679, 0, -3017.6726474369307)


var vertex, i,

        center, p0, p1, angle,

        radius, startAngle,

        thetaAngle;

    THREE.Geometry.call(this);

    this.startPoint = p0 = startPoint ? new THREE.Vector2(startPoint.x, startPoint.y) : new THREE.Vector2(0, 0);

    this.endPoint = p1 = endPoint ? new THREE.Vector2(endPoint.x, endPoint.y) : new THREE.Vector2(1, 0);

    this.bulge = bulge = bulge || 1;

    angle = 4 * Math.atan(bulge);

    radius = p0.distanceTo(p1) / 2 / Math.sin(angle / 2);

    center = THREEx.Math.polar(startPoint, radius, THREEx.Math.angle2(p0, p1) + (Math.PI / 2 - angle / 2));

    this.segments = segments = segments || Math.max(Math.abs(Math.ceil(angle / (Math.PI / 18))), 6); // By default want a segment roughly every 10 degrees

    startAngle = THREEx.Math.angle2(center, p0);

    thetaAngle = angle / segments;

    this.vertices.push(new THREE.Vector3(p0.x ? p0.x : 0, p0.y ? p0.y : 0, 0));

    for (i = 1; i <= segments - 1; i++) {

        vertex = THREEx.Math.polar(center, Math.abs(radius), startAngle + thetaAngle * i);

        this.vertices.push(new THREE.Vector3(vertex.x ? vertex.x : 0, vertex.y ? vertex.y : 0, 0));

    }

bulge - a value indicating how much to curve

bulge = -0.4142135623726088

Here it looks like this formula is for x,y plane but my geometries are in x.z plane

THREEx.Math.polar = function (point, distance, angle) {

    var result = {};

    result.x = point.x + distance * Math.cos(angle);

    result.y = point.y + distance * Math.sin(angle);

    return result;

};

THREEx.Math.angle2 = function (p1, p2) {

    var v1 = new THREE.Vector2(p1.x, p1.y);

    var v2 = new THREE.Vector2(p2.x, p2.y);

    v2.sub(v1); // sets v2 to be our chord

    v2.normalize();

    if (v2.y < 0) return -Math.acos(v2.x);

    return Math.acos(v2.x);

};

Please let me know if its possible

There are infinitely many curves between two points. It is not clear to me which constraints you assume to get a unique solution. There are several very well described types of curves. Maybe you can make a sketch?

One request - format your code clearly. To do this, mark the individual passages with the mouse and then format them with
2021-08-18 21.03.30
as code.

see
2021-08-18 21.03.51

:slightly_smiling_face:

2 Likes

Hi @hofk ,

Sorry for the code format and thanks for quick reply.

So here i am attaching the files that contain the points and also the shape i want to get.


points.txt (12.8 KB)

Here is the code file that algo.txt (4.2 KB)

Here issue is the bulge part is working for x,y plane but i am into x,z

So need to adjust this algo in x.z plane

Now the thing becomes clearer.
In points.txt you can see that only some corners are rounded. This is also visible in the image of the shape.
From the formulas it can be seen that part of a circle is used to connect the lines.


THREEx.Math.polar = function (point, distance, angle) {
    var result = {};
    result.x = point.x + distance * Math.cos(angle);
    result.y = point.y + distance * Math.sin(angle);
    return result;
};

distance is radius

Whether x,y or x,z is needed is not a big difference, you just have to exchange the variables y and z and put them in the corresponding position in 3D.

Create a live example (e.g. jsfiddle) where the lines are also generated. Then you can see where errors may occur.

Only on the basis of the limited section it is difficult to give concrete hints.

This is my fiddle link Edit fiddle - JSFiddle - Code Playground, after replacing it with z also its not giving me exact result. The fiddle is not so proper because i am using it first time , but it give you an Idea what i am trying.

This is what i am getting

This is what i am expecting in x and z plane

Tried to create the figure on the picture provided: Edit fiddle - JSFiddle - Code Playground

Hi @prisoner849 ,

I always appreciate your help and i like you send some fiddle as solution. But my issue here is
I have to use the points that i am getting from the cad files if you check in the fiddle there is an entity obj with some bulge parameter (means how the curve is) and i have to follow the same algorithm as it is working in my app for other parts but i am not able to understand why i am not getting the proper shapes for curvy polygon.

Hello @prisoner849 ,

I am attaching an file that have a working example. Please let me know if any one can provide me help to understand why the shape is not proper.polygon.html (6.4 KB)

Could you clarify, what and how does that bulge define?

    {
        "x": 8.816292322934935,
        "y": 5.51136731706687,
        "bulge": -0.414213562373095
    }

Its a geometry of dxf file and this parser is parsing the geometry

dxf-parser

This is available in npm , here it is givimg me geometry in js obj form.

I just asked about a property and what it defines, and you offer me to read about a whole dxf-format and investigate its parser :slight_smile:
That’s cute.

Seems, the solution is something like this: Line with rounded corners

1 Like

thats how helping forums work, always ) here ,let me narrow it down for you Solved: bulge center DXF .NET - Autodesk Community

Yeah, I’ve seen it yesterday :slight_smile:
Curious, if the author have seen it before me :thinking:

Hello @prisoner849 ,

with your help i am able to solve my bulge problem thanks for that, here my shapes are proper but they are not aligned.

https://jsfiddle.net/fhcmn5ab/6/

Please can you help me in this means how i can translate them so i can see them in my scene as points are really big so translation is needed.


Here i just added 2 set of lines for your reference as fiddle is not working but algo is there, please help me in this also

Thanks

I didn’t get this line: entity.geometry.translate(-avg.x,avg.y,-avg.z), where avg is an object of booleans { x: false, y: false, z: false }, whereas translate() expects numbers for its paramerters.

Here what i am doing is i am using dxf parser and it is giving me huge file and than i am scaling them or adjusting them by using convertDBTO3JS method but still they are huge in thousands and my scene is 0,0,0 so i am using a logic that is calculating the bounding box by using this

if (center.x && avg.x === false) { avg.x = center.x } else if (center.x) { avg.x = (avg.x + center.x) / 2 }
if (center.y && avg.y === false) { avg.y = center.y } else if (center.y) { avg.y = (avg.y + center.y) / 2 }
if (center.z && avg.z === false) { avg.z = center.z } else if (center.z) { avg.z = (avg.z + center.z) / 2 }

or avg value and translating them so they come into my scene and i can see them but this logic is failing

I want to know how i can scale them.

for an example if i get these set of points from my dxf parser

vertices: Array(5)
new THREE.Vector3(11480992.59240649,3454785.183761712,0)
new THREE.Vector3(11481107.78168301,3454732.692403951,0)
new THREE.Vector3(11481100.31350271,3454716.314788647,0)
new THREE.Vector3(11480985.23491298,3454768.755673313,0)
new THREE.Vector3(11480992.59240649,3454785.183761712,0)

and than i am converting them to 3js points so still because i be my formula that is right convertDBTO3js method

i am getting something like this just an example

  1. 0: {x: 3478.191345726834, y: 2584.936039809744}
  2. 1: {x: 3514.91366668384, y: 2570.920569193518}
  3. 2: {x: 3543.756774791946, y: 2556.489211227111}
  4. 3: {x: 3568.707387707588, y: 2538.593241464366}

Now how i can shift them to origin so i can view the shapes and i have to translate them so object become visible to camera

I fixed it, thanks for your support.