Пример #1
0
        void draw_2d_e(Renderer r, Lens lens, Element ref_)
        {
            bool grp = false;

            if (lens.stop() != null)
            {
                draw_2d_e(r, lens.stop(), ref_);
            }

            if (lens.elements().Count == 0)
            {
                return;
            }

            List <OpticalSurface> surfaces = lens.surfaces();
            OpticalSurface        first    = surfaces[0];

            if (first.get_material(1) != first.get_material(0))
            {
                if (!grp)
                {
                    r.group_begin("");
                    grp = true;
                }

                draw_2d_e(r, first, ref_);
            }

            for (int i = 0; i < surfaces.Count - 1; i++)
            {
                OpticalSurface left  = surfaces[i];
                OpticalSurface right = surfaces[i + 1];

                if (left.get_material(1) == null || !(left.get_material(1) is Solid))
                {
                    if (grp)
                    {
                        r.group_end();
                        grp = false;
                    }
                }
                else
                {
                    // draw outter edges
                    double left_top_edge
                        = left.get_shape().get_outter_radius(Vector2.vector2_01);
                    double left_bot_edge
                        = -left.get_shape().get_outter_radius(Vector2.vector2_01.negate());
                    double right_top_edge
                        = right.get_shape().get_outter_radius(Vector2.vector2_01);
                    double right_bot_edge
                        = -right.get_shape().get_outter_radius(Vector2.vector2_01.negate());

                    draw_2d_edge(r, left, left_top_edge, right, right_top_edge,
                                 Lens.LensEdge.StraightEdge, ref_);
                    draw_2d_edge(r, left, left_bot_edge, right, right_bot_edge,
                                 Lens.LensEdge.StraightEdge, ref_);

                    // draw hole edges if not coincident
                    double left_top_hole
                        = left.get_shape().get_hole_radius(Vector2.vector2_01);
                    double left_bot_hole
                        = -left.get_shape().get_hole_radius(Vector2.vector2_01.negate());
                    double right_top_hole
                        = right.get_shape().get_hole_radius(Vector2.vector2_01);
                    double right_bot_hole
                        = -right.get_shape().get_hole_radius(Vector2.vector2_01.negate());

                    if (Math.Abs(left_bot_hole - left_top_hole) > 1e-6 ||
                        Math.Abs(right_bot_hole - right_top_hole) > 1e-6)
                    {
                        draw_2d_edge(r, left, left_top_hole, right, right_top_hole,
                                     Lens.LensEdge.SlopeEdge, ref_);
                        draw_2d_edge(r, left, left_bot_hole, right, right_bot_hole,
                                     Lens.LensEdge.SlopeEdge, ref_);
                    }
                }

                if (right.get_material(1) != right.get_material(0))
                {
                    if (!grp)
                    {
                        r.group_begin("");
                        grp = true;
                    }

                    draw_2d_e(r, right, ref_);
                }
            }

            if (grp)
            {
                r.group_end();
            }
        }
Пример #2
0
        private TracedRay trace_ray_simple(OpticalSurface surface, RayTraceResults result, TracedRay incident,
                                           Vector3Pair local, Vector3Pair intersect)
        {
            bool   right_to_left = intersect.normal().z() > 0;
            Medium prev_mat      = surface.get_material(right_to_left ? 1 : 0);
            Medium next_mat      = surface.get_material(!right_to_left ? 1 : 0);

            // check ray didn't "escaped" from its material
            // std::cout << prev_mat->name << " " << next_mat->name <<
            //          " " << incident.get_material()->name << std::endl;

            if (prev_mat != incident.get_material())
            {
                return(null);
            }

            double wl    = incident.get_wavelen();
            double index = prev_mat.get_refractive_index(wl)
                           / next_mat.get_refractive_index(wl);

            // refracted ray direction
            Vector3 direction = refract(surface, local, intersect.normal(), index);

            if (direction == null)
            {
                // total internal reflection
                Vector3   o   = intersect.origin();
                Vector3   dir = reflect(surface, local, intersect.normal());
                TracedRay r   = result.newRay(o, dir);

                r.set_wavelen(wl);
                r.set_intensity(incident.get_intensity());
                r.set_material(prev_mat);

                r.set_creator(surface);
                incident.add_generated(r);

                return(r);
            }

            // transmit
            if (!next_mat.is_opaque())
            {
                Vector3   o = intersect.origin();
                TracedRay r = result.newRay(o, direction);

                r.set_wavelen(wl);
                r.set_intensity(incident.get_intensity());
                r.set_material(next_mat);

                r.set_creator(surface);
                incident.add_generated(r);
                return(r);
            }

            // reflect
            if (next_mat.is_reflecting())
            {
                Vector3 o   = intersect.origin();
                Vector3 dir = reflect(surface, local, intersect.normal());

                TracedRay r = result.newRay(o, dir);

                r.set_wavelen(wl);
                r.set_intensity(incident.get_intensity());
                r.set_material(prev_mat);
                r.set_creator(surface);
                incident.add_generated(r);
                return(r);
            }

            return(null);
        }