671{
672#ifdef EDGE_GRID_DEBUG_OUTPUT
673 static int iRun = 0;
674 ++ iRun;
675#endif
676
677
678 size_t nrows =
m_rows + 1;
679 size_t ncols =
m_cols + 1;
680
681 std::vector<float>
L(nrows * ncols * 2, FLT_MAX);
682
683
684
685 std::vector<unsigned char> signs(nrows * ncols, 4);
686
687
690
691 for (
int r = 0; r < (int)
m_rows; ++ r) {
692 for (
int c = 0;
c < (int)
m_cols; ++
c) {
694
695 for (size_t i = cell.begin; i != cell.end; ++ i) {
699
702
704
706
707 for (int corner_y = -1; corner_y < 3; ++ corner_y) {
708 coord_t corner_r = r + corner_y;
709 if (corner_r < 0 || (size_t)corner_r >= nrows)
710 continue;
711 for (int corner_x = -1; corner_x < 3; ++ corner_x) {
713 if (corner_c < 0 || (size_t)corner_c >= ncols)
714 continue;
718
720 if (t_pt < 0) {
721
723 if (dabs < d_min) {
724
728 if (t2_pt > 0) {
729
730
732 assert(det != 0);
733 d_min = dabs;
734
735 float *l = &
L[(corner_r * ncols + corner_c) << 1];
736 l[0] = std::abs(v_pt(0));
737 l[1] = std::abs(v_pt(1));
738 #ifdef _DEBUG
739 double dabs2 =
sqrt(l[0]*l[0]+l[1]*l[1]);
740 assert(std::abs(dabs-dabs2) < 1e-4 * std::max(dabs, dabs2));
741 #endif
742 signs[corner_r * ncols + corner_c] = ((det < 0) ? 1 : 0) | 2;
743 }
744 }
745 }
746 else if (t_pt > l2_seg) {
747
748 continue;
749 } else {
750
751 assert(t_pt >= 0 && t_pt <= l2_seg);
753 double d = double(d_seg) /
sqrt(
double(l2_seg));
754 double dabs = std::abs(d);
755 if (dabs < d_min) {
756 d_min = dabs;
757
758 float *l = &
L[(corner_r * ncols + corner_c) << 1];
759 float linv = float(d_seg) / float(l2_seg);
760 l[0] = std::abs(float(v_seg(1)) * linv);
761 l[1] = std::abs(float(v_seg(0)) * linv);
762 #ifdef _DEBUG
763 double dabs2 =
sqrt(l[0]*l[0]+l[1]*l[1]);
764 assert(std::abs(dabs-dabs2) <= 1e-4 * std::max(dabs, dabs2));
765 #endif
766 signs[corner_r * ncols + corner_c] = ((d_seg < 0) ? 1 : 0) | 2;
767 }
768 }
769 }
770 }
771 }
772 }
773 }
774
775#ifdef EDGE_GRID_DEBUG_OUTPUT
776 {
777 std::vector<uint8_t> pixels(ncols * nrows * 3, 0);
778 for (
coord_t r = 0; r < nrows; ++ r) {
780 uint8_t *pxl = pixels.data() + (((nrows - r - 1) * ncols) +
c) * 3;
782 if (d != search_radius) {
783 float s = 255 *
d / search_radius;
784 int is = std::max(0, std::min(255,
int(
floor(s + 0.5f))));
785 pxl[0] = 255;
786 pxl[1] = 255 - is;
787 pxl[2] = 255 - is;
788 }
789 else {
790 pxl[0] = 0;
791 pxl[1] = 255;
792 pxl[2] = 0;
793 }
794 }
795 }
797 }
798 {
799 std::vector<uint8_t> pixels(ncols * nrows * 3, 0);
800 for (
coord_t r = 0; r < nrows; ++ r) {
802 unsigned char *pxl = pixels.data() + (((nrows - r - 1) * ncols) +
c) * 3;
804 if (d != search_radius) {
805 float s = 255 *
d / search_radius;
806 int is = std::max(0, std::min(255,
int(
floor(s + 0.5f))));
807 if ((signs[r * ncols + c] & 1) == 0) {
808
809 pxl[0] = 255;
810 pxl[1] = 255 - is;
811 pxl[2] = 255 - is;
812 }
813 else {
814
815 pxl[0] = 255 - is;
816 pxl[1] = 255 - is;
817 pxl[2] = 255;
818 }
819 }
820 else {
821 pxl[0] = 0;
822 pxl[1] = 255;
823 pxl[2] = 0;
824 }
825 }
826 }
828 }
829#endif
830
831
832 #define PROPAGATE_SIGNUM_SINGLE_STEP(DELTA) do { \
833 size_t addr = r * ncols + c; \
834 unsigned char &cur_val = signs[addr]; \
835 if (cur_val & 4) { \
836 unsigned char old_val = signs[addr + (DELTA)]; \
837 if ((old_val & 4) == 0) \
838 cur_val = old_val & 1; \
839 } \
840 } while (0);
841
842 for (size_t r = 0; r < nrows; ++ r) {
843 if (r > 0)
844 for (
size_t c = 0;
c < ncols; ++
c)
846 for (
size_t c = 1;
c < ncols; ++
c)
848 for (
int c =
int(ncols) - 2;
c >= 0; --
c)
850 }
851
852 for (int r = int(nrows) - 2; r >= 0; -- r) {
853 for (
size_t c = 0;
c < ncols; ++
c)
855 for (
size_t c = 1;
c < ncols; ++
c)
857 for (
int c =
int(ncols) - 2;
c >= 0; --
c)
859 }
860 #undef PROPAGATE_SIGNUM_SINGLE_STEP
861
862
863
864 PropagateDanielssonSingleStep<1, 0> danielsson_hstep(
L.data(), signs.data(), ncols,
m_resolution);
865 PropagateDanielssonSingleStep<0, 1> danielsson_vstep(
L.data(), signs.data(), ncols,
m_resolution);
866 PropagateDanielssonSingleVStep3 danielsson_vstep3(
L.data(), signs.data(), ncols,
m_resolution);
867
868 for (size_t r = 0; r < nrows; ++ r) {
869 if (r > 0)
870 for (
size_t c = 0;
c < ncols; ++
c)
871 danielsson_vstep(r, c, -int(ncols));
872
873 for (
size_t c = 1;
c < ncols; ++
c)
874 danielsson_hstep(r, c, -1);
875 for (
int c =
int(ncols) - 2;
c >= 0; --
c)
876 danielsson_hstep(r, c, +1);
877 }
878
879 for (int r = int(nrows) - 2; r >= 0; -- r) {
880 for (
size_t c = 0;
c < ncols; ++
c)
881 danielsson_vstep(r, c, +ncols);
882
883 for (
size_t c = 1;
c < ncols; ++
c)
884 danielsson_hstep(r, c, -1);
885 for (
int c =
int(ncols) - 2;
c >= 0; --
c)
886 danielsson_hstep(r, c, +1);
887 }
888
889
890 for (size_t r = 0; r < nrows; ++ r) {
891 for (
size_t c = 0;
c < ncols; ++
c) {
892 size_t addr = r * ncols +
c;
893 float *v = &
L[addr<<1];
894 float d =
sqrt(v[0]*v[0]+v[1]*v[1]);
895 if (signs[addr] & 1)
898 }
899 }
900
901#ifdef EDGE_GRID_DEBUG_OUTPUT
902 {
903 std::vector<uint8_t> pixels(ncols * nrows * 3, 0);
905 for (
coord_t r = 0; r < nrows; ++r) {
907 uint8_t *pxl = pixels.data() + (((nrows - r - 1) * ncols) +
c) * 3;
910 case 0:
911
912 pxl[0] = 0;
913 pxl[1] = 0;
914 pxl[2] = 255;
915 break;
916 case 1:
917
918 pxl[0] = 255;
919 pxl[1] = 0;
920 pxl[2] = 0;
921 break;
922 case 2:
923
924 pxl[0] = 100;
925 pxl[1] = 100;
926 pxl[2] = 255;
927 break;
928 case 3:
929
930 pxl[0] = 255;
931 pxl[1] = 100;
932 pxl[2] = 100;
933 break;
934 case 4:
935
936 pxl[0] = 0;
937 pxl[1] = 255;
938 pxl[2] = 0;
939 break;
940 default:
941
942 pxl[0] = 255;
943 pxl[1] = 255;
944 pxl[2] = 255;
945 break;
946 }
947 }
948 }
950 }
951#endif
952
953#ifdef EDGE_GRID_DEBUG_OUTPUT
954 {
955 std::vector<uint8_t> pixels(ncols * nrows * 3, 0);
957 for (
coord_t r = 0; r < nrows; ++r) {
959 uint8_t *pxl = pixels.data() + (((nrows - r - 1) * ncols) +
c) * 3;
961 float s = 255.f * fabs(d) / search_radius;
962 int is = std::max(0, std::min(255,
int(
floor(s + 0.5f))));
963 if (d < 0.f) {
964 pxl[0] = 255;
965 pxl[1] = 255 - is;
966 pxl[2] = 255 - is;
967 }
968 else {
969 pxl[0] = 255 - is;
970 pxl[1] = 255 - is;
971 pxl[2] = 255;
972 }
973 }
974 }
976 }
977#endif
978}
EIGEN_DEVICE_FUNC const SignReturnType sign() const
Definition ArrayCwiseUnaryOps.h:184
EIGEN_DEVICE_FUNC const FloorReturnType floor() const
Definition ArrayCwiseUnaryOps.h:388
EIGEN_DEVICE_FUNC const SqrtReturnType sqrt() const
Definition ArrayCwiseUnaryOps.h:152
#define PROPAGATE_SIGNUM_SINGLE_STEP(DELTA)
PointType min
Definition BoundingBox.hpp:16
std::vector< std::pair< size_t, size_t > > m_cell_data
Definition EdgeGrid.hpp:395
std::vector< Cell > m_cells
Definition EdgeGrid.hpp:398
coord_t m_resolution
Definition EdgeGrid.hpp:385
std::vector< Contour > m_contours
Definition EdgeGrid.hpp:392
size_t m_rows
Definition EdgeGrid.hpp:386
std::vector< float > m_signed_distance_field
Definition EdgeGrid.hpp:402
size_t m_cols
Definition EdgeGrid.hpp:387
int32_t coord_t
Definition libslic3r.h:39
bool write_rgb_to_file_scaled(const char *file_name_utf8, size_t width, size_t height, const uint8_t *data_rgb, size_t scale)
Definition PNGReadWrite.cpp:243
std::string debug_out_path(const char *name,...)
Definition utils.cpp:218
const Polygon & contour(const ExPolygon &p)
Definition AGGRaster.hpp:21
#define L(s)
Definition I18N.hpp:18
__int64 int64_t
Definition unistd.h:76
unsigned __int8 uint8_t
Definition unistd.h:77