bool SDFRenderable::intersect(RayIntersection &intersection) { intersection.object = NULL; Vector3dd walk = intersection.ray.p; double l = 0.0; static const int LIMIT = 100; for (int i = 0; i < LIMIT; i++) { double d = F(walk); l += fabs(d); if (fabs(d) < 0.00000001) { double delta = 1e-7; Vector3dd normal; normal.x() = F(walk + Vector3dd::OrtX() * delta) - d; normal.y() = F(walk + Vector3dd::OrtY() * delta) - d; normal.z() = F(walk + Vector3dd::OrtZ() * delta) - d; normal /= delta; normal.normalise(); intersection.object = this; intersection.normal = normal; intersection.t = l; //SYNC_PRINT(("d=%lf (%lf) (%lf %lf %lf)\n", l, d, normal.x(), normal.y(), normal.z())); return true; } if (fabs(d) > 1e10) { return false; } walk = intersection.ray.getPoint(l); } return false; }
void TestbedMainWindow::preprocessImage(void) { if (mImage == NULL) { return; } L_INFO_P("Starting to preprocess"); PointScene *scene = new PointScene(); //scene->scene.push_back(PointScene::Point(Vector3dd(1.0, 1.0, 1.0))); scene->showBuffer(mImage); m3DHist->setNewScenePointer(QSharedPointer<Scene3D>(scene), CloudViewDialog::MAIN_SCENE); Vector3dd mean(0.0); for (int i = 0; i < mImage->h; i++) { for (int j = 0; j < mImage->w; j++) { mean += mImage->element(i,j).toDouble(); } } mean /= (mImage->h * mImage->w); EllipticalApproximationUnified<Vector3dd> ellip; for (int i = 0; i < mImage->h; i++) { for (int j = 0; j < mImage->w; j++) { ellip.addPoint(mImage->element(i,j).toDouble() - mean); } } ellip.getEllipseParameters(); qDebug() << "Size is: "<< ellip.mAxes.size(); EllApproxScene *sceneEl = new EllApproxScene(mean, ellip); m3DHist->setNewScenePointer(QSharedPointer<Scene3D>(sceneEl), CloudViewDialog::ADDITIONAL_SCENE); L_INFO_P("Color distribution analyzed"); L_INFO_P("Preparing HSV presentation"); delete_safe(mHComp); delete_safe(mSComp); delete_safe(mVComp); mHComp = new G8Buffer(mImage->getSize(), false); mSComp = new G8Buffer(mImage->getSize(), false); mVComp = new G8Buffer(mImage->getSize(), false); for (int i = 0; i < mImage->h; i++) { for (int j = 0; j < mImage->w; j++) { RGBColor &color = mImage->element(i,j); mHComp->element(i,j) = color.hue() * 255 / 360; mSComp->element(i,j) = color.saturation(); mVComp->element(i,j) = color.value(); } } L_INFO_P("Preparing edges"); G12Buffer *tempBuffer = G8Buffer::toG12Buffer(mVComp); delete_safe(mEdges); delete_safe(mCannyEdges); delete_safe(mEdges); CannyParameters *cannyParams = mUi->cannyParametersWidget->createParameters(); DerivativeBuffer *derivativeBuffer = NULL; mCannyEdges = CannyFilter::doFilter(tempBuffer, *cannyParams, &derivativeBuffer); mEdges = derivativeBuffer->gradientMagnitudeBuffer(10.0); delete_safe(derivativeBuffer); delete_safe(cannyParams); delete_safe(tempBuffer); L_INFO_P("Preparing projected buffer"); Vector3dd mainDirection = ellip.mAxes[0]; mainDirection.normalise(); L_INFO << "Principal component is:" << mainDirection; delete_safe(mPrincipal); mPrincipal = projectToDirection(mImage, mainDirection); Vector3dd secondaryDirection = ellip.mAxes[1]; secondaryDirection.normalise(); L_INFO << "Secondary component is:" << secondaryDirection; delete_safe(mPrincipal2); mPrincipal2 = projectToDirection(mImage, secondaryDirection); Vector3dd thirdDirection = ellip.mAxes[2]; thirdDirection.normalise(); L_INFO << "Third component is:" << thirdDirection; delete_safe(mPrincipal3); mPrincipal3 = projectToDirection(mImage, thirdDirection); PreciseTimer timer; L_INFO_P("Preparing local histogram buffer"); timer = PreciseTimer::currentTime(); delete_safe(mHistBuffer); mHistBuffer = new AbstractBuffer<LocalHistogram>(mPrincipal->getSize()); int bound = mUi->histRadiusSpinBox->value(); for (int i = bound; i < mPrincipal->h - bound; i++) { for (int j = bound; j < mPrincipal->w - bound; j++) { for (int dy = -bound; dy <= bound; dy++) { for (int dx = -bound; dx <= bound; dx++) { mHistBuffer->element(i, j).inc(mPrincipal->element(i + dy, j + dx)); } } } } L_INFO_P(" Done in %d", timer.usecsToNow()); L_INFO_P("Preparing local histogram2D buffer"); timer = PreciseTimer::currentTime(); delete_safe(mHist2DBuffer); mHist2DBuffer = new Histogram2DBuffer(mPrincipal->getSize()); bound = mUi->histRadiusSpinBox->value(); for (int i = bound; i < mPrincipal->h - bound; i++) { for (int j = bound; j < mPrincipal->w - bound; j++) { LocalHistogram2D *hist = &mHist2DBuffer->element(i, j); hist->isSet = true; for (int dy = -bound; dy <= bound; dy++) { for (int dx = -bound; dx <= bound; dx++) { hist->inc(mPrincipal->element(i + dy, j + dx), mPrincipal2->element(i + dy, j + dx)); } } } } L_INFO_P(" Done in %d", timer.usecsToNow()); updateViewImage(); }
void BillboardCaption3DScene::drawMyself(CloudViewDialog *dialog) { /*Caption drawing*/ OpenGLTools::GLWrapper wrapper; GLPainter painter(&wrapper); Matrix44 modelview = OpenGLTools::glGetModelViewMatrix(); Matrix44 projection = OpenGLTools::glGetProjectionMatrix(); Matrix44 glMatrix = projection * modelview; /* Setting new matrices */ int width = dialog->mUi.widget->width(); int height = dialog->mUi.widget->height(); /* SetMatrices to draw on 2D canvas */ glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glOrtho(- width / 2.0 , width / 2.0, -height / 2.0, height / 2.0, 10, -10); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); double size = mParameters.fontSize() / 25.0; for (auto caption: mLabels) { /* QString text = QString("[%1, %2, %3]") .arg(centralPoint.x(), 0, 'f', 2).arg(centralPoint.y(), 0, 'f', 2).arg(centralPoint.z(), 0, 'f', 2); */ QString text = QString::fromStdString(caption.second); Vector3dd labelPos = glMatrix * caption.first; if ((!dialog->isOrtho((CloudViewDialog::CameraType)dialog->mUi.cameraTypeBox->currentIndex())) && (labelPos.z() < 0)) { continue; } glPushMatrix(); glTranslated(labelPos[0] * width / 2.0, labelPos[1] * height / 2.0, 0.0); glScaled(size, -size, size); bool depthTest = glIsEnabled(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST); GLint lineWidth; glGetIntegerv(GL_LINE_WIDTH, &lineWidth); glLineWidth(mParameters.fontWidth()); //glColor3ub(mParameters.fontColor().r(), mParameters.fontColor().g(), mParameters.fontColor().b()); painter.drawFormatVector(0, 0, mParameters.fontColor(), 1, text.toLatin1().constData()); if (depthTest) { glEnable(GL_DEPTH_TEST); } glLineWidth(lineWidth); glPopMatrix(); } /*Restore old matrices */ glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopMatrix(); }
int main (int argc, char **argv) { QCoreApplication app(argc, argv); printf("Loading mask...\n"); QTRGB24Loader ::registerMyself(); QTG12Loader ::registerMyself(); QTRuntimeLoader::registerMyself(); QImage imageMask ("data/adopt/orig.png"); QImage imageAlpha ("data/adopt/alpha.bmp"); QImage imageFace ("data/adopt/face.png"); RGB24Buffer *alpha24 = QTFileLoader::RGB24BufferFromQImage(&imageAlpha); RGB24Buffer *mask = QTFileLoader::RGB24BufferFromQImage(&imageMask); RGB24Buffer *face = QTFileLoader::RGB24BufferFromQImage(&imageFace); G8Buffer *alpha = alpha24->getChannel(ImageChannel::GRAY); Vector3dd meanMask(0.0); Vector3dd meanFace(0.0); double count = 0; /* Get hole statistics */ for (int i = 0; i < mask->h; i++) { for (int j = 0; j < mask->w; j++) { if (alpha->element(i,j) > 10) continue; count++; meanFace += face->element(i,j).toDouble(); meanMask += mask->element(i,j).toDouble(); } } meanFace /= count; meanMask /= count; cout << "Mean face value is" << meanFace << endl; cout << "Mean face value is" << meanMask << endl; EllipticalApproximationUnified<Vector3dd> facePrincipal; EllipticalApproximationUnified<Vector3dd> maskPrincipal; for (int i = 0; i < mask->h; i++) { for (int j = 0; j < mask->w; j++) { facePrincipal.addPoint(face->element(i,j).toDouble() - meanFace); maskPrincipal.addPoint(mask->element(i,j).toDouble() - meanMask); } } facePrincipal.getEllipseParameters(); maskPrincipal.getEllipseParameters(); cout << "Face Principals" << endl; cout << facePrincipal.mAxes[0] << "->" << facePrincipal.mValues[0] << endl; cout << facePrincipal.mAxes[1] << "->" << facePrincipal.mValues[1] << endl; cout << facePrincipal.mAxes[2] << "->" << facePrincipal.mValues[2] << endl; cout << "Mask Principals" << endl; cout << maskPrincipal.mAxes[0] << "->" << maskPrincipal.mValues[0] << endl; cout << maskPrincipal.mAxes[1] << "->" << maskPrincipal.mValues[1] << endl; cout << maskPrincipal.mAxes[2] << "->" << maskPrincipal.mValues[2] << endl; Vector3dd scalers; scalers.x() = sqrt(maskPrincipal.mValues[0]) / sqrt(facePrincipal.mValues[0]); scalers.y() = sqrt(maskPrincipal.mValues[1]) / sqrt(facePrincipal.mValues[1]); scalers.z() = sqrt(maskPrincipal.mValues[2]) / sqrt(facePrincipal.mValues[2]); /* Making correction for face */ RGB24Buffer *faceCorr = new RGB24Buffer(face->getSize(), false); for (int i = 0; i < faceCorr->h; i++) { for (int j = 0; j < faceCorr->w; j++) { Vector3dd color = face->element(i,j).toDouble() - meanFace; Vector3dd projected(color & facePrincipal.mAxes[0], color & facePrincipal.mAxes[1], color & facePrincipal.mAxes[2]); projected = projected * scalers; Vector3dd newColor = maskPrincipal.mAxes[0] * projected.x() + maskPrincipal.mAxes[1] * projected.y() + maskPrincipal.mAxes[2] * projected.z() + meanMask; RGBColor newrgb; double c; c = newColor.x(); if (c < 0) c = 0; if (c > 255) c = 255; newrgb.r() = c; c = newColor.y(); if (c < 0) c = 0; if (c > 255) c = 255; newrgb.g() = c; c = newColor.z(); if (c < 0) c = 0; if (c > 255) c = 255; newrgb.b() = c; faceCorr->element(i,j) = newrgb; } } /* With Matrix*/ Matrix33 scalerM = Matrix33::Scale3(scalers); Matrix33 toUnityM = Matrix33::FromRows(facePrincipal.mAxes[0], facePrincipal.mAxes[1], facePrincipal.mAxes[2]); Matrix33 fromUnityM = Matrix33::FromColumns(maskPrincipal.mAxes[0], maskPrincipal.mAxes[1], maskPrincipal.mAxes[2]); Matrix33 transform = fromUnityM * scalerM * toUnityM; RGB24Buffer *faceCorr2 = new RGB24Buffer(face->getSize(), false); for (int i = 0; i < faceCorr2->h; i++) { for (int j = 0; j < faceCorr2->w; j++) { Vector3dd newColor = transform * (face->element(i,j).toDouble() - meanFace) + meanMask; RGBColor newrgb; double c; c = newColor.x(); if (c < 0) c = 0; if (c > 255) c = 255; newrgb.r() = c; c = newColor.y(); if (c < 0) c = 0; if (c > 255) c = 255; newrgb.g() = c; c = newColor.z(); if (c < 0) c = 0; if (c > 255) c = 255; newrgb.b() = c; faceCorr2->element(i,j) = newrgb; } } /* Without roots */ scalers.x() = maskPrincipal.mValues[0] / facePrincipal.mValues[0]; scalers.y() = maskPrincipal.mValues[1] / facePrincipal.mValues[1]; scalers.z() = maskPrincipal.mValues[2] / facePrincipal.mValues[2]; /* Making correction for face */ RGB24Buffer *faceCorr1 = new RGB24Buffer(face->getSize(), false); for (int i = 0; i < faceCorr1->h; i++) { for (int j = 0; j < faceCorr1->w; j++) { Vector3dd color = face->element(i,j).toDouble() - meanFace; Vector3dd projected(color & facePrincipal.mAxes[0], color & facePrincipal.mAxes[1], color & facePrincipal.mAxes[2]); projected = projected * scalers; Vector3dd newColor = maskPrincipal.mAxes[0] * projected.x() + maskPrincipal.mAxes[1] * projected.y() + maskPrincipal.mAxes[2] * projected.z() + meanMask; RGBColor newrgb; double c; c = newColor.x(); if (c < 0) c = 0; if (c > 255) c = 255; newrgb.r() = c; c = newColor.y(); if (c < 0) c = 0; if (c > 255) c = 255; newrgb.g() = c; c = newColor.z(); if (c < 0) c = 0; if (c > 255) c = 255; newrgb.b() = c; faceCorr1->element(i,j) = newrgb; } } /* Make a final blending */ BMPLoader().save("output0.bmp", mask); RGB24Buffer *result = alphaBlend(mask, face, alpha); BMPLoader().save("output1.bmp", result); RGB24Buffer *result1 = alphaBlend(mask, faceCorr, alpha); BMPLoader().save("output2.bmp", result1); RGB24Buffer *result2 = alphaBlend(mask, faceCorr1, alpha); BMPLoader().save("output3.bmp", result2); RGB24Buffer *result3 = alphaBlend(mask, faceCorr2, alpha); BMPLoader().save("matrix-out.bmp", result3); delete_safe(alpha); delete_safe(mask); delete_safe(face); delete_safe(faceCorr); delete_safe(faceCorr1); delete_safe(faceCorr2); delete_safe(result); delete_safe(result1); delete_safe(result2); delete_safe(result3); return 0; }
void OpenGLTools::drawWiredBox(const Vector3dd &p1, const Vector3dd &p2) { glBegin(GL_LINES); /* One side */ glVertex3d(p1.x(), p1.y(), p1.z()); glVertex3d(p1.x(), p2.y(), p1.z()); glVertex3d(p1.x(), p2.y(), p1.z()); glVertex3d(p2.x(), p2.y(), p1.z()); glVertex3d(p2.x(), p2.y(), p1.z()); glVertex3d(p2.x(), p1.y(), p1.z()); glVertex3d(p2.x(), p1.y(), p1.z()); glVertex3d(p1.x(), p1.y(), p1.z()); /* Other side */ glVertex3d(p1.x(), p1.y(), p2.z()); glVertex3d(p1.x(), p2.y(), p2.z()); glVertex3d(p1.x(), p2.y(), p2.z()); glVertex3d(p2.x(), p2.y(), p2.z()); glVertex3d(p2.x(), p2.y(), p2.z()); glVertex3d(p2.x(), p1.y(), p2.z()); glVertex3d(p2.x(), p1.y(), p2.z()); glVertex3d(p1.x(), p1.y(), p2.z()); /*Connections*/ glVertex3d(p1.x(), p1.y(), p1.z()); glVertex3d(p1.x(), p1.y(), p2.z()); glVertex3d(p1.x(), p2.y(), p1.z()); glVertex3d(p1.x(), p2.y(), p2.z()); glVertex3d(p2.x(), p2.y(), p1.z()); glVertex3d(p2.x(), p2.y(), p2.z()); glVertex3d(p2.x(), p1.y(), p2.z()); glVertex3d(p2.x(), p1.y(), p1.z()); glEnd(); }
void OpenGLTools::glTranslateVector3dd(const Vector3dd &p) { glTranslated(p.x(), p.y(), p.z()); }
void OpenGLTools::glVertexVector3dd(const Vector3dd &p) { // glVertex3d(p.x(), p.y(), p.z()); glVertex3dv(&(p.x())); }
void OpenGLTools::drawDiamond(const Vector3dd &p, const double r) { glBegin(GL_TRIANGLES); /**/ glVertex3d(p.x() + r, p.y() , p.z() ); glVertex3d(p.x() , p.y() + r, p.z() ); glVertex3d(p.x() , p.y() , p.z() + r); /**/ glVertex3d(p.x() + r, p.y() , p.z() ); glVertex3d(p.x() , p.y() , p.z() + r); glVertex3d(p.x() , p.y() - r, p.z() ); /**/ glVertex3d(p.x() - r, p.y() , p.z() ); glVertex3d(p.x() , p.y() - r, p.z() ); glVertex3d(p.x() , p.y() , p.z() + r); /**/ glVertex3d(p.x() - r, p.y() , p.z() ); glVertex3d(p.x() , p.y() , p.z() + r); glVertex3d(p.x() , p.y() + r, p.z() ); /**/ /* And bottom part */ /**/ glVertex3d(p.x() + r, p.y() , p.z() ); glVertex3d(p.x() , p.y() + r, p.z() ); glVertex3d(p.x() , p.y() , p.z() - r); /**/ glVertex3d(p.x() + r, p.y() , p.z() ); glVertex3d(p.x() , p.y() , p.z() - r); glVertex3d(p.x() , p.y() - r, p.z() ); /**/ glVertex3d(p.x() - r, p.y() , p.z() ); glVertex3d(p.x() , p.y() - r, p.z() ); glVertex3d(p.x() , p.y() , p.z() - r); /**/ glVertex3d(p.x() - r, p.y() , p.z() ); glVertex3d(p.x() , p.y() , p.z() - r); glVertex3d(p.x() , p.y() + r, p.z() ); /**/ glEnd(); }