CATransform3DScale expecting CATransform3D struct not SCNMatrix4 - scenekit

I am trying to put some 3D text in my app, but I need to scale it, here is the code I'm trying to use:
SCNText *text = [SCNText textWithString:#"Some Text" extrusionDepth:4.f];
SCNNode *textNode = [SCNNode nodeWithGeometry:text];
textNode.position = SCNVector3Make(-1, 5, 0);
textNode.transform = CATransform3DScale(textNode.transform, .1f, .1f, .1f);
[root addChildNode:textNode];
and I get a
CATransform3DScale expecting CATransform3D struct not SCNMatrix4
or something of the sort.
If I don't transform, the text takes up most of the screen.
Any ideas?
Thanks

on OS X SCNMatrix4 is a typedef of CATransform3D (and thus you can use CoreAnimation utils) but that's not true on iOS. Have a look at SceenKitTypes.h, it exposes functions that match the ones of CA such as SCNMatrix4Scale.
Also it's strange that the text appears too big in your screen. The default font size is rather small and you must almost always change the geometry's font size for it to fit well in your scene (changing the font size is better than scaling because the discretization of the glyph changes and lead to smoother curves). Is that text the only thing in your scene?

Related

PDFSharp TextFormatter.DrawString() with fractional font sizes in PDFSharp gives shortened lines

I'm using WPF, and I have a RichTextBox in my user interface, which I convert to a PDF file. I take the RichTextBox.Document FlowDocument from the RichTextBox and translate it to a PdfSharp.Pdf.PdfPage.
This has been working pretty good (after finding some help for wordwrap on SO), but I found that I need to scale the PDF, so after I get the font from the FlowDoc, I multiply it by a scale factor, in my case 0.88.
This appeared to work great, but on closer inspection, I found that a few lines were terminating early.
// these lines use font info from the FlowDoc. To simplify, I've
// hard-coded the font size.
// this works fine:
var thisRunXFont = new XFont(thisRun.FontFamily.Source, 14, xRunFontStyle);
// this causes problems:
var thisRunXFont = new XFont(thisRun.FontFamily.Source, 12.32, xRunFontStyle);
Has anyone else seen this kind of trouble? I do go on to use MeasureString() to get the enclosing paragraph -- but forcing the rectangle to be wider does not change the behavior.

Add SCNText to SCNScene with ARKit

I just started studying for ARKitexample and Scenekit. I read a few Scenekit and found out that in order to add text, I need to use SCNText.
I try to write like this but it doesn't show.
guard let pointOfView = sceneView.pointOfView else { return }
let text = SCNText(string: "Hello", extrusionDepth: 4)
let textNode = SCNNode(geometry: text)
textNode.geometry = text
textNode.position = SCNVector3Make(pointOfView.position.x, pointOfView.position.y, pointOfView.position.z)
sceneView.scene.rootNode.addChildNode(textNode)
I just want to add some text (like "hello world") on SCNScene when user press button.
Edit
I saw that text but since I haven't set up plane (or anchor), I can't look at that as if I am in front of that text. How can I do?
You have at least two problems here.
If you set a node's position to match that of the camera, you probably won't see any of that node's content. You want to position things in front of the camera for them to be seen. A camera always looks in the -z direction of its local space. There's a ton of ways to do the requisite math, but here's one that might be handy (coded on phone, so YMMV):
textNode.simdPosition = pointOfView.simdPosition + pointOfView.simdWorldFront * 0.5
This should put your object half a meter in front of the camera (or rather, where the camera is at that moment — it won't follow the camera). It works because simdWorldFront is the vector (0,0,-1), which in local space means the direction the camera node points, converted from local space to world space.
The default font size for SCNText is something like 16. But that's in scene units, and scene units map to meters in ARKit. Also, the "text box" is anchored at its lower left. So quite likely your text isn't visible because it's sixteen meters tall and off to your right.
An easy way to handle this is by setting a scale or pivot on the node that makes its contents much smaller.

SDL pixel has wrong values

I am trying to create a image processing software.
I get some weird results trying to create an Unsharp Mask effect.
I will attach my code here and I will explain what it does and where the problems are (or at least , where I think they are):
void unsharpMask(SDL_Surface* inputSurface,SDL_Surface* outputSurface)
{
Uint32* pixels = (Uint32*)inputSurface->pixels;
Uint32* outputPixels=(Uint32*)outputSurface->pixels;
Uint32* blurredPixels=(Uint32*)blurredSurface->pixels;
meanBlur(infoSurface,blurredSurface);
for (int i=0;i<inputSurface->h;i++)
{
for(int j=0;j<inputSurface->w;j++)
{
Uint8 rOriginal,gOriginal,bOriginal;
Uint8 rBlurred,gBlurred,bBlurred;
Uint32 rMask,gMask,bMask;
Uint32 rFinal,gFinal,bFinal;
SDL_GetRGB(blurredPixels[i*blurredSurface->w+j],blurredSurface->format,&rBlurred,&gBlurred,&bBlurred);
SDL_GetRGB(pixels[i*inputSurface->w+j],inputSurface->format,&rOriginal,&gOriginal,&bOriginal);
rMask=rOriginal - rBlurred;
rFinal=rOriginal + rMask;
if(rFinal>255) rFinal=255;
if(rFinal<=0) rFinal=0;
gMask=gOriginal - gBlurred;
gFinal=gOriginal + gMask;
if(gFinal>255) gFinal=255;
if(gFinal<0) gFinal=0;
bMask=bOriginal - bBlurred;
bFinal=bOriginal + bMask;
if(bFinal>255) bFinal=255;
if(bFinal<0) bFinal=0;
Uint32 pixel =SDL_MapRGB(outputSurface->format,rFinal,gFinal,bFinal);
outputPixels[i *outputSurface->w+j]=pixel;
}
}
}
So, as you can see, my function gets 2 arguments: the image source(from which pixel data will be extracted, and a target, where the image will be projected). I blur the original image, then i subtract the RGB value of the blurred image from the source image to get "the mask" and then , i add the mask to the original image and that's it. I added some clamping to make sure everything stays in the correct range and then I draw every pixel resulted on the output surface. All these surfaces have been converted in an SDL_PIXELFORMAT_ARGB8888 . The output surface is loaded into a texture (also SDL_PIXELFORMAT_ARGB8888) and rendered on the screen.
The results are pretty good in 90% of the image, I get the effect I want, however , there are some pixels that look weird in some places.
Original:
Result:
I tried to fix this in any possible way I knew. I thought is a format problem and played with the pixel bit depth , but I couldn't get to a good result. What i found is that all the values > 255 are negative values and I tried to make them completely white. And it works for the skies ,for example, but if you can see on my examples, the dark values, on the grass are also affected, which makes this not a good solution.
I also get this kind of wrong pixels when I want to add a contrast or do sharpen using kernel convolution, and the values are really bright/dark.
In my opinion there may be a problem with the pixel format, but I'm not sure if that's true.
Is there anyone that had this kind of problem before or knows a potential solution?

OpenGL saving objects for later drawing

I'm making a program using openGL with transparent objects in it, so obviously I have to paint those last. Unfortunately I was unaware of this requirement when beginning this project and now it would be a real pain to reorder it painting those at last.
I'm drawing objects by calling my drawing functions after translating and rotating the scene. There can be multiple translations and rotations before an actual drawing (e.g. first I draw the ground, then translate, then call the drawing of the house, which repeatedly translates and rotates, then calls the drawing of the walls and so on).
So my idea was saving the current modelview matrices in a list instead of painting the transparent objects when I normally would, then when I'm done with the opaque stuff, I iterate through my list and load each matrix and paint each object (a window, to be precise).
I do this for saving a matrix:
GLdouble * modelMatrix = (GLdouble *)malloc(16 * sizeof(GLdouble));
glGetDoublev(GL_MODELVIEW, modelMatrix);
addWindow(modelMatrix); // save it for later painting
And this is the "transparent stuff management" part:
/***************************************************************************
*** TRANSPARENT STUFF MANAGEMENT ******************************************
**************************************************************************/
typedef struct wndLst {
GLdouble * modelMatrix;
struct wndLst * next;
} windowList;
windowList * windows = NULL;
windowList * pWindow;
void addWindow(GLdouble * windowModelMatrix) {
pWindow = (windowList *)malloc(sizeof(windowList));
pWindow->modelMatrix = windowModelMatrix;
pWindow->next = windows;
windows = pWindow;
}
void clearWindows() {
while(windows != NULL) {
pWindow = windows->next;
free(windows->modelMatrix);
free(windows);
windows = pWindow;
}
}
void paintWindows() {
glPushMatrix(); // I've tried putting this and the pop inside the loop, but it didn't help either
pWindow = windows;
while(pWindow != NULL) {
glLoadMatrixd(pWindow->modelMatrix);
Size s;
s.height = 69;
s.width = 49;
s.length = 0.1;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDepthMask(GL_FALSE);
glColor4f(COLOR_GLASS, windowAlpha);
drawCuboid(s);
glDepthMask(GL_TRUE);
glDisable(GL_BLEND);
pWindow = pWindow->next;
}
glPopMatrix();
}
/* INTERFACE
* paint all the components, that are not yet painted,
* then clean up.
*/
void flushComponents() {
paintWindows();
clearWindows();
}
/**************************************************************************/
I call flushComponents(); at the very end of my drawings.
The problem is, that the windows don't get in their place, instead I get weird-shaped blue objects randomly appearing and disappearing in my scene.
Am I doing something wrong? Or such matrix manipulations cannot even be used like this? Then what other method could I use for doing this?
Here is the full code if you need it: farm.zip Matrix-saving is at components.c line 1548, management is at line 142. It might not work on Windows without some minor hacking with the includes, which should probably be done in global.h.
Edit: I can only use C code and the glut library to write this program.
Edit 2: The problem is glGetDoublev not returning anything for some reason, it leaves the modelMatrix array intact. Though I still have no idea what causes this, I could make a workaround using bernie's idea.
OpenGL is not a math library. You should not use it for doing matrix calculations. In fact that part has been completely removed from OpenGL-3. Instead you should rely on a specialized matrix math library. That allows you to calculate the matrices for each object with ease, without jumping through the hoops of OpenGL's glGet… API (which was never meat for this kind of abuse). For a good replacement look at GLM: http://glm.g-truc.net/
Try adding glMatrixMode(GL_MODELVIEW) before your paintWindows() method. Perhaps you are not modifying the correct matrix.
The basic idea of your method is fine and is very similar to what I used for transparent objects. I would however advise for performance reasons against reading back the matrices from OpenGL. Instead, you can keep a CPU version of the current modelview matrix and just copy that to your window array.
As for your comment about push and pop matrix, you can safely put it outside the loop like you did.
edit
Strictly speaking, your method of rendering transparent objects misses one step: before rendering the list of windows, you should sort them back to front. This allows for overlapping windows to have the correct final color. In general, for 2 windows and a blending function:
blend( blend(scene_color, window0_color, window0_alpha), window1_color, window1_alpha )
!=
blend( blend(scene_color, window1_color, window1_alpha), window0_color, window0_alpha )
However, if all windows consist of the exact same uniform color (e.g. plain texture or no texture) and alpha value, the above equation is true (window0_color==window1_color and window1_alpha==window0_alpha) so you don't need to sort your windows. Also, if it's not possible to have overlapping windows, don't worry about sorting.
edit #2
Now you found something interesting with the erroneous matrix readback. Try it out with the following instead (you certainly don't need double precision):
GLfloat* modelMatrix = (GLfloat*)malloc(16 * sizeof(GLfloat)); // glass
glGetFloatv(GL_MODELVIEW, modelMatrix);
addWindow(modelMatrix); // save it for later painting
If that still doesn't work, you could directly store references to your houses in your transparent object list. During the transparent rendering pass, re-render each house, but only issue actual OpenGL draw calls for the transparent parts. In your code, putWallStdWith would take another boolean argument specifying whether to render the transparent or the opaque geometry. This way, your succession of OpenGL matrix manipulation calls would be redone for the transparent parts instead of read using glGetxxx(GL_MODEL_VIEW).
The correct way to do it however is to do matrix computation on the CPU and simply load complete matrices in OpenGL. That allows you to reuse matrices, control the operation precision, see the actual matrices easily, etc.

OpenCV: How to merge two static images into one and emboss text on it?

I have completed an image processing algorithm where I extract certain features from two similar images.
I'm using OpenCV2.1 and I wish to showcase a comparison between these two similar images. I wish to combine both the images into one, where the final image will have both the images next to one another. Like in the figure below.
Also, the black dots are the similarities my algorithm has found, now I want to mark them with digits. Where, point 1 on the right is the corresponding matching point on the left.**
What OpenCV functions are useful for this work?
If you really want them in the same window, and assuming they have same width and height (if they are similar they should have same width and height). You could try to create an image with a final width twice bigger than the width of your 2 similar images. And then use ROI to copy them.
You can write a new function to encapsulate these (usefull) functions in one function in order to have a nice code.
Mat img1,img2; //They are previously declared and of the same width & height
Mat imgResult(img1.rows,2*img1.cols,img1.type()); // Your final image
Mat roiImgResult_Left = imgResult(Rect(0,0,img1.cols,img1.rows)); //Img1 will be on the left part
Mat roiImgResult_Right = imgResult(Rect(img1.cols,0,img2.cols,img2.rows)); //Img2 will be on the right part, we shift the roi of img1.cols on the right
Mat roiImg1 = img1(Rect(0,0,img1.cols,img1.rows));
Mat roiImg2 = img2(Rect(0,0,img2.cols,img2.rows));
roiImg1.copyTo(roiImgResult_Left); //Img1 will be on the left of imgResult
roiImg2.copyTo(roiImgResult_Right); //Img2 will be on the right of imgResult
Julien,
The easiest way I can think right now would be to create two windows instead of one. You can do it using cvNamedWindow(), and then position them side by side with cvMoveWindow().
After that if you now the position of the similarities on the images, you can draw your text near them. Take a look at cvInitFont(), cvPutText().

Resources