70001956 How to map an image to drawing units

Article 70001956
Type HowTo
Product Engine
Version 8
Date Added 4/16/2021 12:00:00 AM
Fixed 9.9002.0.1 (4/16/2021 12:00:00 AM)
Submitted by Ville Sipola

Summary

How to map an image to drawing units

Solution

VectorDraw Framework exports the ability to map an image to a vdPolyFace or vdGroundSurface object using the MappedImages property of these objects. An example of this can be found in our AddEntities Sample.
If you want to map a single image over your drawing you can use the following example code:

Suppose that we have an image with name "myimage.jpg"
Also we have 2 points in pixels coordinate system relative to the UpperLeft corner of the image

example
    P1_pixel_x , P1_pixel_y
and
    P2_pixel_x , P2_pixel_y

Now we have a drawing where we want to map the image
1.We select the corresponding points for P1_pixel and P2_pixel in the vdraw world coordinate System
Suppose that we select the P1_world for P1_pixel and P2_world for p2_pixel

then we can calculate the matrix that we must apply to the vdImage to be inserted in the vdraw drawing as follow:
vdDocument doc;//already defined VectorDraw Document object.
//construct the gPoints for the selected pixel points
gPoint imgp1 = new gPoint(P1_pixel_x , P1_pixel_y,0);
gPoint imgp2 = new gPoint(P2_pixel_x , P2_pixel_y,0);

//add the imagedefinition to the drawing
vdImageDef imagedef = doc.Images.Add("myimage.jpg" );

double Aspect =(double) imagedef .Height / (double)imagedef .Width;//width, height of the selected image in pixels

//create a matrix that converts the pixel coordinates of the image to a Unint width matix
Matrix pixeltoworld = new Matrix();
pixeltoworld.ScaleMatrix(1.0 / magedef .Width, -Aspect /imagedef .Height, 1.0d);
pixeltoworld.TranslateMatrix(0.0, Aspect, 0.0);
//convert pixell coords to unit world coord
imgp1 = pixeltoworld.Transform(imgp1);
imgp2 = pixeltoworld.Transform(imgp2);

Vector offsrc = new Vector(imgp2 - imgp1);//Sorce vector

Vector offdst =new Vector(P2_world -P1_world );//destinasion vector

double scale = offdst.Length / offsrc.Length;

//calcualte final transformation matrix for the mapped image
Matrix m2 = new Matrix();
m2.TranslateMatrix(imgp1 * -1);
m2.ScaleMatrix(scale, scale, 1.0);
m2.RotateZMatrix(offdst.Angle2DDirection() - offsrc.Angle2DDirection());
m2.TranslateMatrix(P1_world );

//create a new image reference the selected "myimage.jpg"
vdImage img2 = new vdImage(doc);
img2.ImageDefinition = imagedef ;
img2.PenColor = new vdColor(Color.White, 100);//make the image transparent

img2.Transformby(m2);//transform the image over the right place in the drawing

doc.Model.Entities.AddItem(img2);//add the image to the model drawing entities

doc.Redraw(true);//force a redraw for the drawing

Send comments on this topic.