Issue with RayCast picking algorithm
I'm trying to get a ray direction so I can pick vertices using my mouse. The following code works when rotating on the X or Y axis independently, it goes slightly off course when the view is rotated on X and Y axis, and the Z axis has no effect whatsoever. I don't know what I'm missing, I expected the inverse view to correct for the z axis ass well. Any help is appreciated.
static Vec3 RayCast3(Vec2 _homogeneousMouseCoordinates, float _fov, float _near, float _far, float _aspect, Mat4 _view)
{
float dx = tanf(_fov*0.5f) * _homogeneousMouseCoordinates.x *_aspect;
float dy = tanf(_fov*0.5f) * _homogeneousMouseCoordinates.y;
Vec3 p1 = { dx*_near, dy*_near, _near };
Vec3 p2 = { dx*_far, dy*_far, _far };
bool valid;
Mat3 invRotView = Mat3::GetInverse(Mat4::ExtractRotate(_view), valid);
return Vec3::Normalize((p2 * invRotView)  (p1 * invRotView));
}
See also questions close to this topic

Turn warnings into error only within supplied diff
At a company I used to work at, the build system was set up to turn warnings into errors only within changed code. It worked by supplying generating a diff (typically between the branch you were trying to merge and
master
and then supplying that diff to some compilation tool, and the tool would produce warnings only within the supplied diff.This was great as it allowed you to e.g. deprecate some function, and have the build system prevent people from introducing new uses of that function, and then remove old usages of that function later.
Unfortunately, I didn't look at the setup closely enough before I left the company, and don't know how to replicate it. My question: How can I replicate this setup?
Question is tagged Clang but I would also be interested in answers that use tooling from other compilers.

Applied Algorithms
Write a code to implement the stack as a class (from classroom) and add a function to the class witch takes a sentence ( array of characters) put it into a stack of characters and modifies the sentence(stack)such that after each word you should add an '@'. Then print the sentence in the regular order

C++ : invalid operands to binary expression when using chrono library
I am trying to print out the time using the code below:
auto my_time = std::chrono::system_clock::now(); std::cout << "My Time is " << my_time << std::endl;
But get the following errors:
error: invalid operands to binary expression ('basic_ostream<char, std::__1::char_traits<char> >' and 'std::__1::chrono::time_point<std::__1::chrono::system_clock, std::__1::chrono::duration<long long, std::__1::ratio<1, 1000000> > >') std::cout << "My Time is " << my_time << std::endl; ~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~ /Users/edamame/external/clang/darwin/include/c++/v1/ostream:218:20: note: candidate function not viable: no known conversion from 'std::__1::chrono::time_point<std::__1::chrono::system_clock, std::__1::chrono::duration<long long, std::__1::ratio<1, 1000000> > >' to 'const void *' for 1st argument; take the address of the argument with & basic_ostream& operator<<(const void* __p); ^ /Users/edamame/external/clang/darwin/include/c++/v1/type_traits:4957:3: note: candidate function not viable: no known conversion from 'basic_ostream<char, std::__1::char_traits<char> >' to 'std::byte' for 1st argument operator<< (byte __lhs, _Integer __shift) noexcept
Any idea what I did wrong? Thanks!

Raytracing and CUDA
I have a raytracing model where I fire 20k rays against a mesh object with ~100k triangular faces.
To calculate coordinates of intersection point I wrote this function based on Mollertrumbore algorithm (https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm):
void MT_intersection(std::vector<double> origin, std::vector<double> dir, std::vector<double> v0, std::vector<double> v1, std::vector<double> v2, std::vector<double> &int_point) { double eps = 0.0000001; std::vector<double> E1(3); std::vector<double> E2(3); std::vector<double> s(3); for (int i = 0; i < 3; i++) { E1[i] = v1[i]  v0[i]; E2[i] = v2[i]  v0[i]; s[i] = origin[i]  v0[i]; } std::vector<double> h(3); h[0] = dir[1] * E2[2]  dir[2] * E2[1]; h[1] = (dir[0] * E2[2]  dir[2] * E2[0]); h[2] = dir[0] * E2[1]  dir[1] * E2[0]; double a; a = E1[0] * h[0] + E1[1] * h[1] + E1[2] * h[2]; if (a > eps && a < eps) { int_point[0] = false; } else { double f = 1 / a; double u; u = f * (s[0] * h[0] + s[1] * h[1] + s[2] * h[2]); if (u < 0  u > 1) { int_point[0] = false; } else { std::vector<double> q(3); q[0] = s[1] * E1[2]  s[2] * E1[1]; q[1] = (s[0] * E1[2]  s[2] * E1[0]); q[2] = s[0] * E1[1]  s[1] * E1[0]; double v; v = f * (dir[0] * q[0] + dir[1] * q[1] + dir[2] * q[2]); if (v < 0  (u + v)>1) { int_point[0] = false; } else { double t; t = f * (E2[0] * q[0] + E2[1] * q[1] + E2[2] * q[2]); if (t > eps) { for (int i = 0; i < 3; i++) { int_point[i] = origin[i] + dir[i] * t; } } } } } }
I give as input the origin and the direction of my ray and 3 vectors with triangles vertices coordinates (v0,v1,v2).
Then I use this function in a for loop with ~100k (number of triangles) repetitions inside another for loop with 20k (number of rays) repetitions.
Since this code is very slow (it takes approx 2 days and a half to calculate everything), I want to run it in parallel with Cuda, hoping to reduce this time. Since I'm working with Python, I'm using PyCuda and I tried to write a C kernel with my "MT_intersection" function:
import pycuda.driver as drv import pycuda.autoinit from pycuda.compiler import SourceModule import numpy as np from stl import mesh my_mesh = mesh.Mesh.from_file('sfera1.stl') n = my_mesh.normals v0 = my_mesh.v0 v1 = my_mesh.v1 v2 = my_mesh.v2 mod = SourceModule(""" #include <math.h> //#include <vector> __global__ void intersect(float *origin,float *dir,float *v0,float *v1,float *v2,float *int_point_real) { using namespace std; //#include <vector> //#include <math.h> int idx = threadIdx.x; //a[idx] *= 2; int count = 0; //std::vector<double> v0_current(3); float v0_current[3]; float v1_current[3]; float v2_current[3]; float dir_current[3] = {dir[idx][0],dir[idx][1],dir[idx][2]}; //std::vector<double> v1_current(3); //std::vector<double> v2_current(3); float int_point[3]; //std::vector<float> int_point(3); //std::vector<std::vector<float>> int_pointS; float int_pointS[2][3]; //std::vector<std::vector<double>> int_point; //std::vector<int> int_faces; int int_faces[2]; float dist[2]; //std::vector<float> dist; int n_tri = 960; for(int i = 0; i<n_tri; i++) { for (int j = 0; j<3; j++){ v0_current[j] = v0[i][j]; v1_current[j] = v1[i][j]; v2_current[j] = v2[i][j]; } double eps = 0.0000001; //std::vector<float> E1(3); float E1[3]; //std::vector<float> E2(3); float E2[3]; //std::vector<float> s(3); float s[3]; for (int j = 0; j < 3; j++) { E1[j] = v1_current[j]  v0_current[j]; E2[j] = v2_current[j]  v0_current[j]; s[j] = origin[j]  v0_current[j]; } //std::vector<float> h(3); float h[3]; h[0] = dir[1] * E2[2]  dir[2] * E2[1]; h[1] = (dir[0] * E2[2]  dir[2] * E2[0]); h[2] = dir[0] * E2[1]  dir[1] * E2[0]; float a; a = E1[0] * h[0] + E1[1] * h[1] + E1[2] * h[2]; if (a > eps && a < eps) { int_point[0] = false; //return false; } else { double f = 1 / a; float u; u = f * (s[0] * h[0] + s[1] * h[1] + s[2] * h[2]); if (u < 0  u > 1) { int_point[0] = false; //return false; } else { //std::vector<float> q(3); float q[3]; q[0] = s[1] * E1[2]  s[2] * E1[1]; q[1] = (s[0] * E1[2]  s[2] * E1[0]); q[2] = s[0] * E1[1]  s[1] * E1[0]; float v; v = f * (dir[0] * q[0] + dir[1] * q[1] + dir[2] * q[2]); if (v < 0  (u + v)>1) { int_point[0] = false; //return false; } else { float t; t = f * (E2[0] * q[0] + E2[1] * q[1] + E2[2] * q[2]); if (t > eps) { for (int j = 0; j < 3; j++) { int_point[j] = origin[j] + dir_current[j] * t; } //return t; } } } } if (int_point[0] != false) { count = count+1; //int_faces.push_back(i); int_faces[count1] = i; //dist.push_back(sqrt(pow((origin[0]  int_point[0]), 2) + pow((origin[1]  int_point[1]), 2) + pow((origin[2]  int_point[2]), 2))); //dist.push_back(x); dist[count1] = sqrt(pow((origin[0]  int_point[0]), 2) + pow((origin[1]  int_point[1]), 2) + pow((origin[2]  int_point[2]), 2)); //int_pointS.push_back(int_point); for (int j = 0; j<3; j++) { int_pointS[count1][j] = int_point[j]; } } } double min = dist[0]; int ind_min = 0; for (int i = 0; i < int_pointS.size(); i++){ if (min > dist[i]) { min = dist[i]; ind_min = i; } } //dist_real[Idx] = dist[ind_min]; //int_point_real_x[Idx] = int_pointS[ind_min][0]; //int_point_real_y[Idx] = int_pointS[ind_min][1]; //int_point_real_z[Idx] = int_pointS[ind_min][2]; int_point_real[Idx][0] = int_pointS[ind_min][0]; int_point_real[Idx][1] = int_pointS[ind_min][1]; int_point_real[Idx][2] = int_pointS[ind_min][2]; } """) origin = np.asarray([1, 1, 1]).astype(np.float32) direction = np.ones((100, 3)).astype(np.float32) int_point_real = np.zeros((100, 3)).astype(np.float32) intersect = mod.get_function("intersect") intersect(drv.In(origin), drv.In(direction), drv.In(v0), drv.In(v1), drv.In(v2), drv.Out(int_point_real), block=(512,1,1), grid=(64,1,1))
My idea is to run the 20k rays in parallel. This Python script is giving me different errors:
kernel.cu(18): error: expression must have pointertoobject type
kernel.cu(18): error: expression must have pointertoobject type
kernel.cu(18): error: expression must have pointertoobject type
kernel.cu(34): error: expression must have pointertoobject type
kernel.cu(35): error: expression must have pointertoobject type
kernel.cu(36): error: expression must have pointertoobject type
kernel.cu(108): error: expression must have class type
kernel.cu(118): error: expression must have pointertoobject type
kernel.cu(119): error: expression must have pointertoobject type
kernel.cu(120): error: expression must have pointertoobject type
kernel.cu(27): warning: variable "int_faces" was set but never used
10 errors detected in the compilation of "C:/Users/20180781/AppData/Local/Temp/tmpxft_00000d44_0000000010_kernel.cpp1.ii". ]
Any idea why?
Does anyone know a smarter and more efficient way to calculate the intersection point when I have a lot of rays and a lot of faces?
 How to make a 3D game with ray casting in Java?

Unity Raycast when hover and press
how to use raycast when hover and press on object. The only method I could come up with is using 2 raycast. But only 1 raycast works, depending on the order in which they are checked.
if (Input.touchCount == 1) { Touch touch = Input.touches[0]; if (touch.phase == TouchPhase.Began) { Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0)); Ray ray1 = Camera.main.ScreenPointToRay(Input.GetTouch(0).position); RaycastHit hit = new RaycastHit(); if(Physics.Raycast(ray, out hit, 5)) if(Physics.Raycast(ray1, out hit, 5)) { ... } } }