Emulate shading of a parameteric surface using Cairo graphics - graphics

A "cushion shaded" treemap is parametric surface that is a sum of parabolic curves. Each node in the tree (including internal nodes) is represented as a parabolic surface and the tree is rendered as the sum of all surfaces. The result is a nice sense of depth because the darkness between nodes is proportional to their height in the tree.
The original paper [pdf] implements a simple raster rendering. I would like to render to a vector format to make my output scalable. How do I emulate shading on a piece-wise quadratic surface using Cairo's gradient mesh?
I think each shaded quad could be converted to four bezier-sided triangles with the curvature of the sides proportional to some ratio of the lighting angle and the surface normal, but I'm really not sure how to implement that transform.

Related

How to apply ear clipping algorithm on a closed polygon, like a sphere?

I've been working on a project that needs to triangulate a sphere. I've known ear-clipping algorithm and know how to apply it in a non-closed polygon, like a plane.
But how to triangulate a closed polygon, like a sphere?
I'm assuming from your question that you want to panel a spherical surface with 3D patches. I would start by sketching a 2D equilateral triangle. Get some practice segmenting it into 4 half-size equilaterals, then each of those into 4 more, etc.
In 3D, you can do the same recursive triangulation on the 4 faces of a regular tetrahedron. Except, if each vertex is normalized to unit length, then you inflate your triangulated polyhedron into a sphere of triangular patches. You can use these patches to render a solid surface rendering (you'll want to learn how a simulated light source and shading works( e.g. Lambertian, Goraud, Phong ). Or, you can draw the edges of the patches as line segments to create a wireframe mesh.

Algorithm to calculate and display a ribbon on a 3D triangle mesh

I am looking for an algorithm for the following problem:
Given:
A 3D triangle mesh. The mesh represents a part of the surface of the earth.
A polyline (a connected series of line segments) whose vertices are always on an edge or on a vertex of a triangle of the mesh. The polyline represents the centerline of a road on the surface of the earth.
I need to calculate and display the road i.e. add half of the road's width on each side of the center line, calculate the resulting vertices in the corresponding triangles of the mesh, fill the area of the road and outline the sides of the road.
What is the simplest and/or most effective strategy to do this? How do I store the data of the road most efficiently?
I see 2 options here:
render thick polyline with road texture
While rendering polyline you need TBN matrix so use
polyline tangent as tangent
surface normal as normal
binormal=tangent x normal
shift actual point p position to
p0=p+d*binormal
p1=p-d*binormal
and render textured line (p0,p1). This approach is not precise match to surface mesh so you need to disable depth or use some sort of blending. Also on sharp turns it could miss some parts of a curve (in that case you can render rectangle or disc instead of line.
create the mesh by shifting polyline to sides by half road size
This produces mesh accurate road fit, but due to your limitations the shape of the road can be very distorted without mesh re-triangulation in some cases. I see it like this:
for each segment of road cast 2 lines shifted by half of road size (green,brown)
find their intersection (aqua dots) with shared edge of mesh with the current road control point (red dot)
obtain the average point (magenta dot) from the intersections and use that as road mesh vertex. In case one of the point is outside shared mesh ignore it. In case both intersections are outside shared edge find closest intersection with different edge.
As you can see this can lead to serious road thickness distortions in some cases (big differences between intersection points, or one of the intersection points is outside surface mesh edge).
If you need accurate road thickness then use the intersection of the casted lines as a road control point instead. To make it possible either use blending or disabling Depth while rendering or add this point to mesh of the surface by re-triangulating the surface mesh. Of coarse such action will also affect the road mesh and you need to iterate few times ...
Another way is use of blended texture for road (like sprites) and compute the texture coordinate for the control points. If the road is too thick then thin it by shifting the texture coordinate ... To make this work you need to select the most far intersection point instead of average ... Compute the real half size of the road and from that compute texture coordinate.
If you get rid of the limitation (for road mesh) that road vertex points are at surface mesh segments or vertexes then you can simply use the intersection of shifted lines alone. That will get rid of the thickness artifacts and simplify things a lot.

Generating density map for tree growth rings

I was just wondering if someone know of any papers or resources on generating synthetic images of growth rings in trees. Im thinking 2d scalar-fields or some other data representation which can then be used to render growth rings like images :)
Thanks!
never done or heard about this ...
If you need simulation then search for biology/botanist sites instead.
If you need just visually close results then I would:
make a polygon covering the cut (circle/oval like shape)
start with circle and when all working try to add some random distortion or use ellipse
create 1D texture with the density
it will be used to fill the polygon via triangle fan. So first find an image of the tree type you want to generate for example this:
Analyze the color and intensity as a function of diameter so extract a pie like piece (or a thin rectangle)
and plot a graph of R,G,B values to see how the rings are shaped
then create function that approximate that (or use piecewise interpolation) and create your own texture as function of tree age. You can interpolate in this way booth the color and density of rings.
My example shows that for this tree the color is the same so only its intensity changes. In this case you do not need to approximate all 3 functions. The bumps are a bit noisy due to another texture layer (ignore this at start). You can use:
intensity=A*|cos(pi*t)| as a start
A is brightness
t is age in years/cycles (and also the x coordinate (scaled) in your 1D texture)
so take base color R,G,B multiply it by A for each t and fill the texture pixel with this color. You can add some randomness to ring period (pi*t) and also the scale can be matched more closely. This is linear growth ,... so you can use exponential instead or interpolate to match bumps per length affected by age (distance form t=0)...
now just render the polygon
mid point is the t=0 coordinate in texture each vertex of polygon is t=full_age coordinate in texture. So render the triangle fan with these texture coordinates. If you need more close match (rings are not the same thickness along the perimeter) then you can convert this to 2D texture
[Notes]
You can also do this incrementally so do just one ring per iteration. Next ring polygon is last one enlarged or scaled by scale>1 and add some randomness, but this needs to be rendered by QUAD STRIP. You can have static texture for single ring so interpolate just the density and overall brightness:
radius(i)=radius(i-1)+ring_width=radius(i-1)*scale
so:
scale=(radius(i-1)+ring_width)/radius(i-1)

Depth sorting triangles in static vector 3D engine

I'm trying to write a 3D renderer for a scientific application which is based on vectors rather than pixels. The idea is to be able to output to vector formats, such as SVG, so I would like to keep everything as vector objects, rather than pixelizing. The objects will often have transparency.
At the moment I decompose everything into 3D triangles and line segments and split where there are overlaps. The scene is then projected and painted with depth-sorting (painter's algorithm). I'm sorting by the minimum depth of the triangle (secondarily sorting by the maximum for ties). This fails when you have long thin triangles behind bigger triangles which can rise in the order.
The scene is only drawn once for the same set of objects. I can't obviously use z-buffering because of the vectorization and transparency. Is there a robust and reasonably fast method of drawing the triangles in the correct z order? Typically there could be a few 1000 triangles.

Affine warp of rectangle

I need to warp imaginary rectangle lying on the image.
So I think I need:
Detect which pixels of images belong to rectangle (something like rasterization?).
Do warp of pixels and somehow do interpolation in rectangle (I don't know how) between pixels.
How to deal with border pixels of belonging to different rectangles?
Generally I trying to do something like this
For warping the images, the following procedure can be applied.
Assuming that you have the displacements of each of the points on the lattice, you need to do a B-Spline interpolation(based on the displacements of the neighboring lattice points) to deform the source image.
For obtaining the optimal displacement of each lattice point, you can use a label set corresponding to the displacement of the lattice point in x-y direction and compute SSD between patches in the source and the target image for different labels. For a smooth solution, a regularization prior needs to be added, so that neighboring points on the lattice have a similar displacement. This joint optimization problem can be solved using MRFs.

Resources