polygons with alpha

A forum dedicated to WPF version of LightningChart Ultimate.

Moderator: Arction_LasseP

juergen
Posts: 27
Joined: Tue Feb 04, 2014 8:11 am

polygons with alpha

Post by juergen » Tue Feb 04, 2014 10:53 am

Hi,
I play a little bit with LC to check if it satisfied our needs.
My plan is to get a view with XYZ positions and intensity data that should be represented with different colors. To get a view inside I have to use trancaprency colors. At the moment I think it's best to use Polygon3D.
It looks like this:
Image

It looks not bad and it's quite fast. (52020 polygons)

But if I rotate the scene it looks ugly:
Image

It's some time ago I programmed directx but is it a problem of the backface culling or the zOrder of the polygons?
If I swap the polygons and create them from back to front I can see it correctly from back side?

Thanks,
Jürgen

User avatar
ArctionPasi
Posts: 1365
Joined: Tue Mar 26, 2013 10:57 pm
Location: Finland

Re: polygons with alpha

Post by ArctionPasi » Tue Feb 04, 2014 11:48 am

The polygons are rendered in the order they appear in the View3D.Polygons collection. Alpha test is made vs. already rendered items in the backbuffer. Reversing the polygons order in the collection is most likely needed to see it correctly from behind.
LightningChart Support Team, PT

juergen
Posts: 27
Joined: Tue Feb 04, 2014 8:11 am

Re: polygons with alpha

Post by juergen » Tue Feb 04, 2014 12:20 pm

So I have to add a sorting algorythm to sort the polygons depending on the distance of the camera position.
Creating the scene completely new each time takes to much time.

User avatar
ArctionPasi
Posts: 1365
Joined: Tue Mar 26, 2013 10:57 pm
Location: Finland

Re: polygons with alpha

Post by ArctionPasi » Tue Feb 04, 2014 12:40 pm

The 3D polygon items can be as they are, just sorting the list should be enough.
By quickly thinking, sorting by camera distance is needed when
- camera rotates (View3D.CameraViewChanged)
- when axis range changes, (axis.ValueRanged)
- Dimensions change (View3D.DimensionsChanged).

Don't sort it again every time you set new data. I believe your data is kind of cubic, MxNxL and therefor all the Polygons are pretty much in their positions. Setting new polygon colors should be enough.

The Polygons collection is a BindingList, which means every time your sort an item, it will cause lot of overhead for chart. That can be avoided.

Create a regular List<Polygon3D> yourPolygonList = new List<Polygon3D>(count);
Fill it from your polygon objects. Then sort that list. Assign the sorted list then like this:
chart.View3D.Polygons = new BindingList<Polygon3D>(yourPolygonList);

When you calculate the distance from camera, avoid taking square root which is an expensive operation. Because absolute distance is not needed, comparison can be made without it.
LightningChart Support Team, PT

juergen
Posts: 27
Joined: Tue Feb 04, 2014 8:11 am

Re: polygons with alpha

Post by juergen » Tue Feb 04, 2014 2:01 pm

I do it now with

Code: Select all

List<Polygon3D> list = m_chart.View3D.Polygons.ToList();
list.Sort(ComparePolygons);
owner.Polygons = new System.ComponentModel.BindingList<Polygon3D>(list); 
I compare absolute positions from a one time calculated camera position with the center of the polygons with this:

Code: Select all

static int ComparePolygons(Polygon3D poly1, Polygon3D poly2)
{
    if (Owner == null)
        return 0;

    Vector3D vec1 = new Vector3D((poly1.Points[0].X + poly1.Points[2].X) / 2, (poly1.YMax + poly1.YMin) / 2, (poly1.Points[0].Z + poly1.Points[1].Z) / 2);
    Vector3D vec2 = new Vector3D((poly2.Points[0].X + poly2.Points[2].X) / 2, (poly2.YMax + poly2.YMin) / 2, (poly2.Points[0].Z + poly2.Points[1].Z) / 2);
    Vector3D diff1 = Vector3D.Subtract(camPosition, vec1);
    Vector3D diff2 = Vector3D.Subtract(camPosition, vec2);
    if (diff1.Length == diff2.Length)
        return 0;

    if (diff1.Length < diff2.Length)
        return -1;
    else
        return 1;
}
It works like a charm.

Thank you ...

User avatar
ArctionPasi
Posts: 1365
Joined: Tue Mar 26, 2013 10:57 pm
Location: Finland

Re: polygons with alpha

Post by ArctionPasi » Tue Feb 04, 2014 3:30 pm

This code seems to have a small problem. The polygon points are in axis values, not 3D world coordinates. It may look like it works when you have a lot of data there, but I'd advice to use the following method for comparison instead. It converts the polygon points to 3D world coordinates before comparison.

Code: Select all


PointDouble3D p1 = m_chart.View3D.ConvertSeriesValueTo3DWorldCoord(poly1, (poly1.Points[0].X + poly1.Points[2].X) / 2, (poly1.YMax + poly1.YMin) / 2, (poly1.Points[0].Z + poly1.Points[1].Z) / 2);

PointDouble3D p2 = m_chart.View3D.ConvertSeriesValueTo3DWorldCoord(poly2, (poly2.Points[0].X + poly2.Points[2].X) / 2, (poly2.YMax + poly2.YMin) / 2, (poly2.Points[0].Z + poly2.Points[1].Z) / 2);

double dDistSquared1 = (camPosition.X - p1.X) * (camPosition.X - p1.X) + (camPosition.Y - p1.Y) * (camPosition.Y - p1.Y) + (camPosition.Z - p1.Z) * (camPosition.Z - p1.Z);
double dDistSquared2 = (camPosition.X - p2.X) * (camPosition.X - p2.X) + (camPosition.Y - p2.Y) * (camPosition.Y - p2.Y) + (camPosition.Z - p2.Z) * (camPosition.Z - p2.Z);

if (dDistSquared1 == dDistSquared2)
                return 0;
if (dDistSquared1 < dDistSquared2)
                return -1;
return 1; 
LightningChart Support Team, PT

juergen
Posts: 27
Joined: Tue Feb 04, 2014 8:11 am

Re: polygons with alpha

Post by juergen » Wed Feb 05, 2014 10:40 am

ConvertSeriesValueTo3DWorldCoord needs SeriesBase3D as first parameter.
Is there another chance to get World Coordinates in a simple way?

User avatar
ArctionPasi
Posts: 1365
Joined: Tue Mar 26, 2013 10:57 pm
Location: Finland

Re: polygons with alpha

Post by ArctionPasi » Wed Feb 05, 2014 10:55 am

So it seems, sorry about that. Polygon3D doesn't inherit from SeriesBase3D.

You can create a dummy/invisible/empty PointLineSeries3D object when you create your chart, and pass that for all ConvertSeriesValueTo3DWorldCoord calls. I think there's no simpler way.
LightningChart Support Team, PT

juergen
Posts: 27
Joined: Tue Feb 04, 2014 8:11 am

Re: polygons with alpha

Post by juergen » Wed Feb 05, 2014 11:21 am

It works perfectly.
Additionally I reduced the polygons by checking the trancparency level. If it's to high it makes no sense to display it.
Now it looks nice.

Thanks a lot ...