void ScreenLayoutManager::UpdateHUDCanvasSize( void ) { const int renderWidth = gEnv->pRenderer->GetWidth(); const int renderHeight = gEnv->pRenderer->GetHeight(); RENDER_SCREEN_WIDTH = (float)renderWidth; RENDER_SCREEN_HEIGHT = (float)renderHeight; assert(RENDER_SCREEN_WIDTH>0.0f); assert(RENDER_SCREEN_HEIGHT>0.0f); INV_RENDER_SCREEN_WIDTH = 1.0f*__fres(RENDER_SCREEN_WIDTH); INV_RENDER_SCREEN_HEIGHT = 1.0f*__fres(RENDER_SCREEN_HEIGHT); // Force update of HUDAssets and other objects. const float pa = gEnv->pRenderer->GetPixelAspectRatio(); Vec2 curCanvasSize( RENDER_SCREEN_WIDTH*__fres(pa), RENDER_SCREEN_HEIGHT ); SHUDEvent resizeEvent(eHUDEvent_OnResolutionChange); resizeEvent.AddData(SHUDEventData(curCanvasSize.x)); resizeEvent.AddData(SHUDEventData(curCanvasSize.y)); resizeEvent.AddData(SHUDEventData(RENDER_SCREEN_WIDTH)); resizeEvent.AddData(SHUDEventData(RENDER_SCREEN_HEIGHT)); resizeEvent.AddData(SHUDEventData(int_round(RENDER_SCREEN_WIDTH))); resizeEvent.AddData(SHUDEventData(int_round(RENDER_SCREEN_HEIGHT))); //resizeEvent.AddData(SHUDEventData(pa)); CHUDEventDispatcher::CallEvent(resizeEvent); }
void Canvas_Delegate_Riemann_Mapping::set_image_parameters() { //std::cout << "Entering Canvas_Delegate_Riemann_Mapping::set_image_parameters" << std::endl; int image_width = image_->width(), image_height = image_->height(); double widths_ratio = image_width*1.0/3.0; double heights_ratio = image_height*1.0/3.0; *image_scale_ = (widths_ratio > heights_ratio) ? widths_ratio : heights_ratio;//heights_ratio : widths_ratio; *image_origin_x_in_pixels_ = int_round(image_width/2.0); *image_origin_y_in_pixels_ = int_round(image_height/2.0); image_width_ = image_->width(); image_height_ = image_->height(); image_data_ = (QRgb*) image_->bits(); return; }
void compute_orientation_histogram(Array<T, N, 1>& orientation_histogram, const Image<Matrix<T,2,1>>& grad_polar_coords, T x, T y, T s, T patch_truncation_factor = T(3), T blur_factor = T(1.5)) { // Weighted histogram of gradients. orientation_histogram.setZero(); // Rounding of the coordinates. auto rounded_x = int_round(x); auto rounded_y = int_round(y); // std deviation of the gaussian weight (cf. [Lowe, IJCV 2004]) auto sigma = s*blur_factor; // Patch radius on which the histogram of gradients is performed. auto patch_radius = int_round(sigma*patch_truncation_factor); // Accumulate the histogram of orientations. for (auto v = -patch_radius; v <= patch_radius; ++v) { for (auto u = -patch_radius; u <= patch_radius; ++u) { if (rounded_x + u < 0 || rounded_x + u >= grad_polar_coords.width() || rounded_y + v < 0 || rounded_y + v >= grad_polar_coords.height()) continue; auto mag = grad_polar_coords(rounded_x+u, rounded_y+v)(0); auto ori = grad_polar_coords(rounded_x+u, rounded_y+v)(1); // ori is in \f$]-\pi, \pi]\f$, so translate ori by \f$2*\pi\f$ if it is // negative. ori = ori < 0 ? ori + T(2.*M_PI) : ori; auto bin_index = int(floor(ori / T(2 * M_PI) * N)); bin_index %= N; // Give more emphasis to gradient orientations that lie closer to the // keypoint location. auto weight = exp(-(u*u + v*v) / (T(2)*sigma*sigma)); // Also give more emphasis to gradient with large magnitude. orientation_histogram(bin_index) += weight*mag; } } }
void Canvas_Delegate_Riemann_Mapping::draw_background_image() { QRect source(0, 0, image_width_, image_height_); complex_number left_top_corner, right_bottom_corner; int top, left, width, height; width = int_round(image_width_*scale_/(*image_scale_)); height = int_round(image_height_*scale_/(*image_scale_)); if (width > height) { left_top_corner = complex_number(-1.5, image_height_*0.5/(*image_scale_)); right_bottom_corner = complex_number(1.5, -image_height_*0.5/(*image_scale_)); } else { left_top_corner = complex_number(- image_width_*0.5/(*image_scale_), 1.5); right_bottom_corner = complex_number(image_width_*0.5/(*image_scale_), - 1.5); } integer_coordinates left_top_corner_in_pixels = complex_to_pixels_coordinates(left_top_corner); integer_coordinates right_bottom_corner_in_pixels = complex_to_pixels_coordinates(right_bottom_corner); //affine_transform_.set_for_match_triangles_in_pixels(&scale_, &origin_x_in_pixels_, &origin_y_in_pixels_, // image_scale_, image_origin_x_in_pixels_, image_origin_y_in_pixels_); //affine_transform_.match_coordinates_in_pixels(); //integer_coordinates left_top = affine_transform_.kick(left_top_corner_in_pixels); left = left_top_corner_in_pixels.first; top = left_top_corner_in_pixels.second; width = right_bottom_corner_in_pixels.first - left; height = right_bottom_corner_in_pixels.second - top; QRect target(left, top, width, height); /*std::cout << "width = " << width << ", " << "height = " << height << ", " << "top = " << top << ", " << "left = " << left << ", " << "ox = " << *image_origin_x_in_pixels_ << ", " << "oy = " << *image_origin_y_in_pixels_ << std::endl;*/ painter_->setOpacity(0.5); painter_->drawImage(target, *image_, source); painter_->setOpacity(1.0); return; }
void Canvas::wheelEvent(QWheelEvent *event) { int nb = 5; int i; for(i = 0; i < nb; i++) { integer_coordinates point_coordinates_in_pixels(event->x(), event->y()); canvas_delegate_->wheel_event(int_round(event->delta()*1.0/nb), point_coordinates_in_pixels); update(); } return; }
void Canvas_Delegate_Riemann_Mapping::draw_triangle_from_image(const Triangle &T) { //std::cout << "Entering Canvas_Delegate_Riemann_Mapping::draw_triangle_from_image" << std::endl;7 bool flat_base_bool = false; bool flat_top_bool = false; integer_coordinates xy_A, xy_B, xy_C, xy_D; Point A, B, C; T.get_ABC_by_decreasing_y(A, B, C); xy_A = complex_to_pixels_coordinates(A.get_affix()); xy_B = complex_to_pixels_coordinates(B.get_affix()); xy_C = complex_to_pixels_coordinates(C.get_affix()); if(xy_B.second == xy_A.second) { flat_base_bool = true; xy_D = xy_A; } else if(xy_B.second == xy_C.second) { flat_top_bool = true; xy_D = xy_C; } else { double inv_pente_AC = (xy_C.first-xy_A.first)*1.0/(xy_C.second-xy_A.second); int x_D = int_round((xy_B.second-xy_A.second)*inv_pente_AC + xy_A.first); xy_D = integer_coordinates(x_D,xy_B.second); } if(xy_D.first < xy_B.first) { std::swap(xy_B,xy_D); } int i, j; double i1, i2; double inv_pente_AB = (xy_B.first-xy_A.first)*1.0/(xy_B.second-xy_A.second); double inv_pente_AD = (xy_D.first-xy_A.first)*1.0/(xy_D.second-xy_A.second); double inv_pente_CB = (xy_B.first-xy_C.first)*1.0/(xy_B.second-xy_C.second); double inv_pente_CD = (xy_D.first-xy_C.first)*1.0/(xy_D.second-xy_C.second); double ops1 = xy_A.first-xy_A.second*inv_pente_AB; double ops2 = xy_A.first-xy_A.second*inv_pente_AD; double ops3 = xy_C.first-xy_C.second*inv_pente_CB; double ops4 = xy_C.first-xy_C.second*inv_pente_CD; QRgb color; integer_coordinates ij; if(!flat_base_bool) { for(j = std::max(0,xy_A.second); j<=std::min(size_in_pixels_, xy_B.second);j++) { i1 = std::max(j*inv_pente_AB + ops1,0.0); i2 = std::min(j*inv_pente_AD + ops2,size_in_pixels_*1.0); for(i = int_round(i1);i<= int_round(i2);i++) { ij = affine_transform_.kick(integer_coordinates(i,j)); if(ij.first<image_->width() && ij.first >0 && ij.second < image_->height() && ij.second >0) { //painter_->setPen(QColor((image_->pixel(ij.first, ij.second)))); if(get_color_from_image(ij.first, ij.second, color)) { draw_pixel(i,j,color); } //painter_->drawPoint(i,j); } } } } if(!flat_top_bool) { for(j = std::min(size_in_pixels_, xy_C.second); j>=std::max(0,xy_B.second);j--) { i1 = std::max(j*inv_pente_CB + ops3,0.0); i2 = std::min(j*inv_pente_CD + ops4,size_in_pixels_*1.0); for(i = int_round(i1);i<= int_round(i2);i++) { ij = affine_transform_.kick(integer_coordinates(i,j)); if(ij.first<image_->size().width() && ij.first >0 && ij.second < image_->size().height() && ij.second >0) { //painter_->setPen(QColor((image_->pixel(ij.first, ij.second)))); //painter_->drawPoint(i,j); if(get_color_from_image(ij.first, ij.second, color)) { draw_pixel(i,j,color); } } } } } return; }
int rounded_sign(T x) { return sign_int(int_round(x)); }
//! @brief Computes the SIFT descriptor for keypoint \$(x,y,\sigma,\theta)\f$. descriptor_type operator()(float x, float y, float sigma, float theta, const Image<Vector2f>& grad_polar_coords) const { const float pi = static_cast<float>(M_PI); /* The oriented keypoint is denoted by $k = (x,y,\sigma,\theta)$. SIFT describes keypoint $k$ in a similarity-invariant manner. To do so, we consider a square image patch which: - is centered in $(x,y)$ - has an orientation angle $\theta$ w.r.t. the image frame coordinates: => to ensure rotation invariance - has a side length proportional to the scale $\sigma$: => to ensure scale invariance This square patch is denoted by $P(x,y,\sigma,\theta) = P(k)$. The square patch $P(x,y,\sigma,\theta)$ is itself divided into NxN smaller square patches $(P_{i,j})_{1 \leq i \leq N, j \leq j \leq N}$. Notice that we omit the variables $(x,y,\sigma,\theta)$ which the patches $P_{i,j}$ actually depend on. $N$ corresponds to the template argument 'int N' which should be 4 as stated in the paper [Lowe, IJCV 2004]). In the image, each small square patch $P_{i,j}$ has a side length $l$ proportional to the scale $\sigma$ of the keypoint, i.e., $l = \lambda \sigma$. */ const float lambda = _bin_scale_unit_length; const float l = lambda*sigma; /* It is important to note that $\lambda$ is some 'universal' constant used for all SIFT descriptors to ensure the scale-invariance of the descriptor. */ /* Now in each image square patch $P_{i,j}$, we build a histogram of gradient orientations $\mathbf{h}_{i,j} \in \mathbb{R}^d$, which quantizes the gradient orientations into $O$ principal orientations. $O$ corresponds to the template argument 'int O'. Let us initialize the SIFT descriptor consisting of the NxN histograms $\mathbf{h}_{i,j}$, each in $\mathbf{R}^O$ as follows. */ descriptor_type h{ descriptor_type::Zero() }; /* In the rescaled and oriented coordinate frame bound to the patch $P(k)$, - keypoint $k$ is located at (0,0) - centers $C_{i,j}$ of patch $P_{i,j}$ are located at $[ -(N+1)/2 + i, -(N+1)/2 + j ]$ For example for $N=4$, they are at: (-1.5,-1.5) (-0.5,-1.5) (0.5,-1.5) (1.5,-1.5) (-1.5,-0.5) (-0.5,-0.5) (0.5,-0.5) (1.5,-0.5) (-1.5, 0.5) (-0.5, 0.5) (0.5, 0.5) (1.5, 0.5) (-1.5, 1.5) (-0.5, 1.5) (0.5, 1.5) (1.5, 1.5) Gradients in $[x_i-1, x_i+1] \times [y_i-1, y_i+1]$ contributes to histogram $\mathbf{h}_{i,j}$, namely gradients in the square patch $Q_{i,j}$ - centered in $C_{i,j}$ as square patch $P_{i,j}$, - with side length $2$. That is because we want to do trilinear interpolation in order to make SIFT robust to small shift in rotation, translation. Therefore, to compute the SIFT descriptor we need to scan all the pixels on a larger circular image patch with radius $r$: */ const float r = sqrt(2.f) * l * (N + 1) / 2.f; /* In the above formula, notice: - the factor $\sqrt{2}$ because diagonal corners of the furthest patches $P_{i,j}$ from the center $(x,y)$ must be in the circular patch. - the factor $(N+1)/2$ because we have to include the gradients in larger patches $Q_{i,j}$ for each $P_{i,j}$. I recommend to make a drawing to convince oneself. */ // To build the SIFT descriptor, we do the following procedure: // - we work in the image reference frame; // - we scan in the convolved image $G_\sigma$ the position $(x+u, y+v)$ // where $(u,v) \in [-r,r]^2$; // - we retrieve its coordinates in the oriented frame of the patch // $P(x,y,\sigma,\theta)$ with inverse transform $T = 1/l R_\theta^T$ Matrix2f T; T << cos(theta), sin(theta), -sin(theta), cos(theta); T /= l; // Loop to perform interpolation const int rounded_r = int_round(r); const int rounded_x = int_round(x); const int rounded_y = int_round(y); for (int v = -rounded_r; v <= rounded_r; ++v) { for (int u = -rounded_r; u <= rounded_r; ++u) { // Compute the coordinates in the rescaled and oriented coordinate // frame bound to patch $P(k)$. Vector2f pos{ T*Vector2f(u, v) }; // subpixel correction? /*pos.x() -= (x - rounded_x); pos.y() -= (y - rounded_y);*/ if ( rounded_x+u < 0 || rounded_x+u >= grad_polar_coords.width() || rounded_y+v < 0 || rounded_y+v >= grad_polar_coords.height() ) continue; // Compute the Gaussian weight which gives more emphasis to gradient // closer to the center. float weight = exp(-pos.squaredNorm()/(2.f*pow(N/2.f, 2))); float mag = grad_polar_coords(rounded_x+u, rounded_y+v)(0); float ori = grad_polar_coords(rounded_x+u, rounded_y+v)(1) - theta; ori = ori < 0.f ? ori+2.f*pi : ori; ori *= float(O)/(2.f*pi); // The coordinate frame is centered in the patch center, thus: // $(x,y)$ is in $[-(N+1)/2, (N+1)/2]^2$. // // Change the coordinate frame so that $(x,y)$ is in $[-1, N]^2$. Thus, // translate by $[ (N-1)/2, (N-1)/2 ]$. pos.array() += N/2.f - 0.5f; if (pos.minCoeff() <= -1.f || pos.maxCoeff() >= static_cast<float>(N)) continue; // In the translated coordinate frame, note that for $N=4$ the centers // are now located at: // (0,0) (1,0) (2,0) (3,0) // (0,1) (1,1) (2,1) (3,1) // (0,2) (1,1) (2,2) (3,2) // (0,3) (1,1) (2,3) (3,3) // // Update the SIFT descriptor using trilinear interpolation. accumulate(h, pos, ori, weight, mag); } } h.normalize(); h = (h * 512.f).cwiseMin(Matrix<float, Dim, 1>::Ones()*255.f); return h; }
virtual void ProcessEvent( EFlowEvent event, SActivationInfo *pActInfo ) { switch (event) { case eFE_Initialize: break; case eFE_Activate: IGameFramework* pGameFramework = gEnv->pGame->GetIGameFramework(); if(IsPortActive(pActInfo, EIP_Cast)) { // setup ray + optionally skip 1 entity ray_hit rayHit; static const float maxRayDist = 100.f; const unsigned int flags = rwi_stop_at_pierceable|rwi_colltype_any; IPhysicalEntity *skipList[1]; int skipCount = 0; IEntity* skipEntity = gEnv->pEntitySystem->GetEntity(GetPortEntityId(pActInfo, EIP_SkipEntity)); if(skipEntity) { skipList[0] = skipEntity->GetPhysics(); skipCount = 1; } Vec3 rayPos = GetPortVec3(pActInfo, EIP_RayPos); Vec3 rayDir = GetPortVec3(pActInfo, EIP_RayDir); // Check if the ray hits an entity if(gEnv->pSystem->GetIPhysicalWorld()->RayWorldIntersection(rayPos, rayDir * 100, ent_all, flags, &rayHit, 1, skipList, skipCount)) { int type = rayHit.pCollider->GetiForeignData(); if (type == PHYS_FOREIGN_ID_ENTITY) { IEntity* pEntity = (IEntity*)rayHit.pCollider->GetForeignData(PHYS_FOREIGN_ID_ENTITY); IEntityRenderProxy* pRenderProxy = pEntity ? (IEntityRenderProxy*)pEntity->GetProxy(ENTITY_PROXY_RENDER) : 0; // Get the renderproxy, and use it to check if the material is a DynTex, and get the UIElement if so if(pRenderProxy) { IRenderNode *pRenderNode = pRenderProxy->GetRenderNode(); IMaterial* pMaterial = pRenderProxy->GetRenderMaterial(); SEfResTexture* texture = 0; if(pMaterial && pMaterial->GetShaderItem().m_pShaderResources) texture= pMaterial->GetShaderItem().m_pShaderResources->GetTexture(EFTT_DIFFUSE); IUIElement* pElement = texture ? gEnv->pFlashUI->GetUIElementByInstanceStr(texture->m_Name) : 0; if(pElement && pRenderNode) { int m_dynTexGeomSlot = 0; IStatObj* pObj = pRenderNode->GetEntityStatObj(m_dynTexGeomSlot); // result bool hasHit = false; Vec2 uv0, uv1, uv2; Vec3 p0, p1, p2; Vec3 hitpos; // calculate ray dir CCamera cam = gEnv->pRenderer->GetCamera(); if (pEntity->GetSlotFlags(m_dynTexGeomSlot) & ENTITY_SLOT_RENDER_NEAREST) { ICVar *r_drawnearfov = gEnv->pConsole->GetCVar("r_DrawNearFoV"); assert(r_drawnearfov); cam.SetFrustum(cam.GetViewSurfaceX(),cam.GetViewSurfaceZ(),DEG2RAD(r_drawnearfov->GetFVal()),cam.GetNearPlane(),cam.GetFarPlane(), cam.GetPixelAspectRatio()); } Vec3 vPos0 = rayPos; Vec3 vPos1 = rayPos + rayDir; // translate into object space const Matrix34 m = pEntity->GetWorldTM().GetInverted(); vPos0 = m * vPos0; vPos1 = m * vPos1; // walk through all sub objects const int objCount = pObj->GetSubObjectCount(); for (int obj = 0; obj <= objCount && !hasHit; ++obj) { Vec3 vP0, vP1; IStatObj* pSubObj = NULL; if (obj == objCount) { vP0 = vPos0; vP1 = vPos1; pSubObj = pObj; } else { IStatObj::SSubObject* pSub = pObj->GetSubObject(obj); const Matrix34 mm = pSub->tm.GetInverted(); vP0 = mm * vPos0; vP1 = mm * vPos1; pSubObj = pSub->pStatObj; } IRenderMesh* pMesh = pSubObj ? pSubObj->GetRenderMesh() : NULL; if (pMesh) { const Ray ray(vP0, (vP1-vP0).GetNormalized() * maxRayDist); hasHit = RayIntersectMesh(pMesh, pMaterial, pElement, ray, hitpos, p0, p1, p2, uv0, uv1, uv2); } } // skip if not hit if (!hasHit) { ActivateOutput(pActInfo, EOP_Failed, 1); return; } // calculate vectors from hitpos to vertices p0, p1 and p2: const Vec3 v0 = p0-hitpos; const Vec3 v1 = p1-hitpos; const Vec3 v2 = p2-hitpos; // calculate factors const float h = (p0-p1).Cross(p0-p2).GetLength(); const float f0 = v1.Cross(v2).GetLength() / h; const float f1 = v2.Cross(v0).GetLength() / h; const float f2 = v0.Cross(v1).GetLength() / h; // find the uv corresponding to hitpos Vec3 uv = uv0 * f0 + uv1 * f1 + uv2 * f2; // translate to flash space int x, y, width, height; float aspect; pElement->GetFlashPlayer()->GetViewport(x, y, width, height, aspect); int iX = int_round(uv.x * (float)width); int iY = int_round(uv.y * (float)height); // call the function provided if it is present in the UIElement description string funcName = GetPortString(pActInfo, EIP_CallFunction); const SUIEventDesc* eventDesc = pElement->GetFunctionDesc(funcName); if(eventDesc) { SUIArguments arg; arg.AddArgument(iX); arg.AddArgument(iY); pElement->CallFunction(eventDesc->sName, arg); } ActivateOutput(pActInfo, EOP_Success, 1); } } } } ActivateOutput(pActInfo, EOP_Failed, 1); } break; } }