代码拉取完成,页面将自动刷新
// voxel.cpp : Defines the entry point for the console application.
//
#include <cstdlib>
#include <cstdio>
#include <cmath>
#define PI 3.141592653
int const NRAY = 768;
int const NPROJ = 180;
int const IMGSIZE = 512;
void get_rays(int voxel, int *rays, float *weights, int &index)
{
//0号voxel的坐标为(0,0),其他voxel在0号voxel的右下方
//top_left coordinate
int tl_x = voxel % IMGSIZE;
int tl_y = -(voxel / IMGSIZE);
//top_right coordinate
int tr_x = voxel % IMGSIZE + 1;
int tr_y = -(voxel / IMGSIZE);
//down_left coordinate
int dl_x = voxel % IMGSIZE;
int dl_y = -(voxel / IMGSIZE + 1);
//down_right coordinate
int dr_x = voxel % IMGSIZE + 1;
int dr_y = -(voxel / IMGSIZE + 1);
int center_x = IMGSIZE / 2;
int center_y = -(IMGSIZE / 2);
//平行线Ax+By+C1=0和Ax+By+C2=0之间的距离为abs(C1-C2)/(A^2+B^2)^0.5
//经过右上角的直线方程为cos(i)y+sin(i)*x-(cos(i)*tr_y + sin(i)*tr_x)=0
//经过中心点的直线方程为cos(i)y+sin(i)*x-(cos(i)*center_y + sin(i)*center_x)=0
//当射线为偶数时上半部分的最后一条射线的方程为 cos(i)*y + sin(i)*x - (cos(i)*center_y + sin(i)*center_x + 0.5)=0
//0-90度角先经过右上角
for (int i = 0; i <= 90; ++i)
{
float distance = (cos(PI*i / 180))*tr_y + sin(PI*i / 180)*tr_x - (cos(PI*i / 180)*center_y + sin(PI*i / 180)*center_x + 0.5); //经过右上角的直线与上半部分的最后一条直线的距离,距离为正表示该直线在上半部分;
int id = ceil((NRAY / 2 - 1) - distance);
rays[index] = i*NRAY + id;
if (i == 0 || i == 90){
weights[index] = 1;
index++;
continue;
}
//经过该像素的第一条射线 cos(i)y + sin(i)x - (cos(i)*center_y + sin(i)*center_x + 0.5 + (NRAY/2-1)-id)=0
//该直线与像素在上边界和下边界的交点位置比较
float x_up_sin = (cos(i*PI / 180) * center_y + sin(i*PI / 180) * center_x + 0.5 + (NRAY / 2 - 1) - id) - cos(i*PI / 180)*tr_y;
float x_bot_sin = (cos(i*PI / 180) * center_y + sin(i*PI / 180) * center_x + 0.5 + (NRAY / 2 - 1) - id) - cos(i*PI / 180)*dr_y;
//该直线与像素在左边界和右边界的交点位置比较
float y_left_cos = (cos(i*PI / 180) * center_y + sin(i*PI / 180) * center_x + 0.5 + (NRAY / 2 - 1) - id) - sin(i*PI / 180)*tl_x;
float y_right_cos = (cos(i*PI / 180) * center_y + sin(i*PI / 180) * center_x + 0.5 + (NRAY / 2 - 1) - id) - sin(i*PI / 180)*tr_x;
//0-90度的角度里,射线只有可能从左边缘和上边缘进入像素点,然后从右边缘和下边缘出去,共有四种情况
if (y_left_cos <= tl_y * cos(i*PI / 180) && x_bot_sin <= dr_x*sin(i*PI / 180)) //左边缘进入,下边缘出
weights[index] = (y_left_cos / cos(i*PI / 180) - dl_y) / sin(i*PI / 180);
else if (y_left_cos <= tl_y*cos(i*PI / 180) && x_bot_sin > dr_x*sin(i*PI / 180)) //左边缘进入,右边缘出
weights[index] = 1 / cos(i*PI / 180);
else if (y_left_cos > tl_y*cos(i*PI / 180) && x_bot_sin <= dr_x*sin(i*PI / 180)) //上边缘进入,下边缘出
weights[index] = 1 / sin(i*PI / 180);
else if (y_left_cos > tl_y*cos(i*PI / 180) && x_bot_sin > dr_x*sin(i*PI / 180)) //上边缘进入,右边缘出
weights[index] = 1 / cos(i*PI / 180) - (y_left_cos / cos(i*PI / 180) - tl_y) / sin(i*PI / 180);
index++;
//经过该像素可能的第二条射线 cos(i)y + sin(i)x - (cos(i)*center_y + sin(i)*center_x + 0.5 + (NRAY/2-1)-id-1)=0
//该直线与像素在上边界和下边界的交点位置比较
x_bot_sin = (cos(i*PI / 180) * center_y + sin(i*PI / 180) * center_x + 0.5 + (NRAY / 2 - 1) - id - 1) - cos(i*PI / 180)*dr_y;
x_up_sin = (cos(i*PI / 180) * center_y + sin(i*PI / 180) * center_x + 0.5 + (NRAY / 2 - 1) - id - 1) - cos(i*PI / 180)*tr_y;
//该直线与像素在左边界和右边界的交点位置比较
y_left_cos = (cos(i*PI / 180) * center_y + sin(i*PI / 180) * center_x + 0.5 + (NRAY / 2 - 1) - id - 1) - sin(i*PI / 180)*tl_x;
y_right_cos = (cos(i*PI / 180) * center_y + sin(i*PI / 180) * center_x + 0.5 + (NRAY / 2 - 1) - id - 1) - sin(i*PI / 180)*tr_x;
//不存在第二条射线的情况
if (y_left_cos <= dl_y *cos(i*PI / 180))
continue;
//存在第二条射线的情况,第二条射线不可能从从上边缘进入
rays[index] = rays[index - 1] + 1;
if (y_left_cos <= tl_y * cos(i*PI / 180) && x_bot_sin <= dr_x*sin(i*PI / 180)) //左边缘进入,下边缘出
weights[index] = (y_left_cos / cos(i*PI / 180) - dl_y) / sin(i*PI / 180);
else if (y_left_cos <= tl_y*cos(i*PI / 180) && x_bot_sin > dr_x*sin(i*PI / 180)) //左边缘进入,右边缘出
weights[index] = 1 / cos(i*PI / 180);
index++;
}
//90-180度先经过右下角
for (int i = 91; i < 180; ++i)
{
float distance = (cos(PI*i / 180))*dr_y + sin(PI*i / 180)*dr_x - (cos(PI*i / 180)*center_y + sin(PI*i / 180)*center_x + 0.5);//经过右下角的直线与上半部分的最后一条直线的距离,距离为正表示该直线在上半部分;
int id = ceil((NRAY / 2 - 1) - distance);
rays[index] = i*NRAY + id;
//经过该像素的第一条射线 cos(i)y + sin(i)x - (cos(i)*center_y + sin(i)*center_x + 0.5 + (NRAY/2-1)-id)=0
//该直线与像素在上边界和下边界的交点位置比较
float x_up_sin = (cos(i*PI / 180) * center_y + sin(i*PI / 180) * center_x + 0.5 + (NRAY / 2 - 1) - id) - cos(i*PI / 180)*tr_y;
float x_bot_sin = (cos(i*PI / 180) * center_y + sin(i*PI / 180) * center_x + 0.5 + (NRAY / 2 - 1) - id) - cos(i*PI / 180)*dr_y;
//该直线与像素在左边界和右边界的交点位置比较
float y_left_cos = (cos(i*PI / 180) * center_y + sin(i*PI / 180) * center_x + 0.5 + (NRAY / 2 - 1) - id) - sin(i*PI / 180)*tl_x;
float y_right_cos = (cos(i*PI / 180) * center_y + sin(i*PI / 180) * center_x + 0.5 + (NRAY / 2 - 1) - id) - sin(i*PI / 180)*tr_x;
//91-180度的角度里,射线只有可能从右边缘和上边缘进入像素点,然后从左边缘和下边缘出去,共有四种情况
if (y_right_cos >= tr_y*cos(i*PI / 180) && x_bot_sin >= dl_x*sin(i*PI / 180)) //右边缘进入,下边缘出
weights[index] = (y_right_cos / cos(i*PI / 180) - dr_y) / sin(i*PI / 180);
else if (y_right_cos >= tr_y*cos(i*PI / 180) && x_bot_sin < dl_x*sin(i*PI / 180)) //右边缘进入,左边缘出
weights[index] = -1 / cos(i*PI / 180);
else if (y_right_cos < tr_y*cos(i*PI / 180) && x_bot_sin >= dl_x*sin(i*PI / 180)) //上边缘进入,下边缘出
weights[index] = 1 / sin(i*PI / 180);
else if (y_right_cos < tr_y*cos(i*PI / 180) && x_bot_sin < dl_x*sin(i*PI / 180)) //上边缘进入,左边缘出
weights[index] = -1 / cos(i*PI / 180) - (y_right_cos / cos(i*PI / 180) - tr_y) / sin(i*PI / 180);
index++;
//经过该像素可能的第二条射线 cos(i)y + sin(i)x - (cos(i)*center_y + sin(i)*center_x + 0.5 + (NRAY/2-1)-id-1)=0
//可能的第二条射线与像素在上边界和下边界的交点位置比较
x_up_sin = (cos(i*PI / 180) * center_y + sin(i*PI / 180) * center_x + 0.5 + (NRAY / 2 - 1) - id - 1) - cos(i*PI / 180)*tr_y;
x_bot_sin = (cos(i*PI / 180) * center_y + sin(i*PI / 180) * center_x + 0.5 + (NRAY / 2 - 1) - id - 1) - cos(i*PI / 180)*dr_y;
//可能的第二条射线与像素在左边界和右边界的交点位置比较
y_left_cos = (cos(i*PI / 180) * center_y + sin(i*PI / 180) * center_x + 0.5 + (NRAY / 2 - 1) - id - 1) - sin(i*PI / 180)*tl_x;
y_right_cos = (cos(i*PI / 180) * center_y + sin(i*PI / 180) * center_x + 0.5 + (NRAY / 2 - 1) - id - 1) - sin(i*PI / 180)*tr_x;
//不存在第二条射线
if (x_up_sin <= tl_x*sin(i*PI / 180))
continue;
//存在第二条射线,第二条射线不可能从右边缘进入
rays[index] = rays[index - 1] + 1;
if (y_right_cos < tr_y*cos(i*PI / 180) && x_bot_sin >= dl_x*sin(i*PI / 180)) //上边缘进入,下边缘出
weights[index] = 1 / sin(i*PI / 180);
else if (y_right_cos < tr_y*cos(i*PI / 180) && x_bot_sin < dl_x*sin(i*PI / 180)) //上边缘进入,左边缘出
weights[index] = -1 / cos(i*PI / 180) - (y_right_cos / cos(i*PI / 180) - tr_y) / sin(i*PI / 180);
index++;
}
}
int main()
{
int voxel=0;
int index=0;
int rays[2 * NPROJ];
float weights[2 * NPROJ];
scanf("%d", &voxel);
get_rays(voxel, rays, weights, index);
printf("%d\n", index);
for (int i = 0; i < index; ++i)
printf("%d ", rays[i]);
printf("\n");
for (int i = 0; i < index; ++i)
printf("%f ", weights[i]);
printf("\n");
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。