Rebuilding polylines

I am doing some openCV stuff in openframeworks and trying to send the resulting line data to TD for rendering and warping, etc, etc.

I’m trying to send 30 lines/contours, each containing 100 xy points. I’m trying to do this over OSC, using the following protocol:

/polyline/polylineNumber x y x y x y x y x y etc…

in this case, the x and y are both floats.
i have this coming into an oscin DAT, but i’m having trouble figuring out how to get dat transformed into points for a SOP.

Any help would be amazing. Thanks in advance.

Hey Jmarsico,

If you’re streaming the data you might have more luck with the OSC In CHOP - values here will come in as floats and won’t need to be type cast from string to float (DATs are always strings). From there you could use the limit SOP to convert your CHOP to a SOP.

If you want to stay in DATs, you might consider looking at the add SOP. This allows you to provide a table of points and convert those points to geometry.

In general, SOPs are a pretty slow place to be doing any transformations or alterations. 100 points might be okay, but if you need to scale up from there you’ll bottleneck at some point. If you’re comfortable with GL you could also use a vertex shader to do these transformations / animations - though you’ll have to get your data into a CHOP or TOP for this kind of approach.

Thanks very much. As is obvious, i’m pretty lost in this new environment. Is there an example or other resource for understanding how to to from :

/lineN x y x y x y x y x y x y

to a format that is ingestable by a SOP?

Alright, a few examples to get you started.
base_polyline_osc.tox (7.04 KB)

First off, ignore the bits on the left hand side - This all simulates your open frameworks pipeline, so you can ignore it safely.

The first example loops through the message and fills in a table that’s used by an add SOP. This is probably the least efficient approach, but is helpful in other ways.

The second example takes that filled in table and then uses a limit SOP to create the geometry.

Example 3 assumes that you’re streaming over the point positions individually. Compared with the first two this is more efficient.

Finally there’s an example that’s highly efficient and done with a vertex shader. This one is a little harder to set-up, but will be the most performant in the long run. Hope this gives you a nudge in the right direction.

*Edits for typos

1 Like

wow. you are too kind. Thank you very much. I had a look on my laptop and will try to incorporate into project this weekend.

This vertex shader idea is amazing; very clever way to encode data. I’m trying to encode multiple lines in a single texture, each row of the texture would represent a different polyline, each column a different xy point in that polyline. Encoded in the r,g channels of the texture.

I’m confused about the use of the texture(sampler, vec2) function and how you’ve used the index variable. Where does that variable come from and would I be able to modify that line to get a texel at a specific xy location in the texture? I feel like I have done this before in a frag shader, but have never worked with vertex shaders.

TL:DR: where does in float index come from and is there a way to modify

vec3 newP = texture(u_linePos, vec2(index/texture_size,0)).rgb;

to give me a color at each xy location? thanks.

Thanks for the props - It’s not my first dance with this challenge :wink:

Geometry attributes can be passed into the vertex stage with an ‘in’ declaration:
derivative.ca/wiki099/index.php? … Attributes

If you look in the geo component you’ll see that there’s a point sop that’s used to add an attribute called ‘index’ to the points. That’s where this comes from.

For multiple lines I’d recommend using instances - having done this lots of was, I think this is the least headache inducing approach. You’d need to pass in a uniform for the total number of instances, but TDInstanceID() can be used to fetch the current instance and you could use this along with a number of total instances to calculate your texture lookup to determine which row of the image you’re in. Something like:

vec2 lookupUV = vec2(index/texture_size, TDInstanceID()/numInstnaces);

That’s off the top of my head, so you might have to noodling a little to make sure that’s correctly sampling the middle of a pixel.

Does that help?

2 Likes