Ejemplo n.º 1
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            int year = 2017;
            //int tasks = 1;
            //if (this.mt) tasks = Environment.ProcessorCount;
            int             tasks   = Environment.ProcessorCount;
            ParallelOptions paropts = new ParallelOptions {
                MaxDegreeOfParallelism = tasks
            };
            //ParallelOptions paropts_1cpu = new ParallelOptions { MaxDegreeOfParallelism = 1 };


            //////////////////////////////////////////////////////////////////////////////////////////
            /// INPUTS
            int reclvl = 0;

            if (!DA.GetData(0, ref reclvl))
            {
                return;
            }

            bool drawviewfactors = false;

            if (!DA.GetData(1, ref drawviewfactors))
            {
                drawviewfactors = false;
            }

            bool drawcumskymatrix = false;

            if (!DA.GetData(2, ref drawcumskymatrix))
            {
                drawcumskymatrix = false;
            }

            bool draw_sunpath = true;

            if (!DA.GetData(3, ref draw_sunpath))
            {
                draw_sunpath = true;
            }

            bool draw_solarvec = true;

            if (!DA.GetData(4, ref draw_solarvec))
            {
                draw_solarvec = true;
            }

            List <int> hoy = new List <int>();

            if (!DA.GetDataList(5, hoy))
            {
                return;
            }

            List <double> loc = new List <double>();

            if (!DA.GetDataList(6, loc))
            {
                return;
            }
            double longitude = loc[0];
            double latitude  = loc[1];

            List <double> dni = new List <double>();
            List <double> dhi = new List <double>();

            DA.GetDataList(7, dni);
            DA.GetDataList(8, dhi);

            double domesize = 1.2;

            if (!DA.GetData(9, ref domesize))
            {
                domesize = 1.2;
            }

            List <Mesh> context = new List <Mesh>();

            DA.GetDataList(10, context);

            Point3d sp = new Point3d();

            if (!DA.GetData(11, ref sp))
            {
                return;
            }


            //////////////////////////////////////////////////////////////////////////////////////////
            /// size of skydome
            Point3d anchor = sp;
            Point3d bb_furthest_point;    // max distance to furthest corner of context
            double  bb_max_distance = double.MinValue;

            if (context.Count > 0)
            {
                Mesh context_joined = new Mesh();
                foreach (Mesh msh in context)
                {
                    context_joined.Append(msh);
                }
                BoundingBox bb_context = context_joined.GetBoundingBox(false);
                if (bb_context.IsDegenerate(-1.0) == 4)
                {
                    bb_furthest_point = new Point3d(sp.X + 1.0, sp.Y + 1.0, sp.Z + 1.0);
                }
                else
                {
                    Point3d[] _pts     = bb_context.GetCorners();
                    int       _p_index = 0;
                    for (int i = 0; i < _pts.Length; i++)
                    {
                        double _d = sp.DistanceTo(_pts[i]);
                        if (_d > bb_max_distance)
                        {
                            bb_max_distance = _d;
                            _p_index        = i;
                        }
                    }
                    bb_furthest_point = _pts[_p_index];
                }
            }
            else
            {
                bb_furthest_point = new Point3d(sp.X + 1.0, sp.Y + 1.0, sp.Z + 1.0);
            }
            Vector3d vec_sp = bb_furthest_point - sp;

            vec_sp = Vector3d.Multiply(vec_sp, domesize);
            double vec_sp_len = vec_sp.Length;


            //////////////////////////////////////////////////////////////////////////////////////////
            /// SKYDOME
            /// View factors and/or Cumulative SkyMatrix
            SkyDome dome = new SkyDome(reclvl);

            // create 2 meshes, one with obstructed views (make it black transparent), and one unobstructed (regular GH_Mesh color)
            Mesh meshObstructed = new Mesh();

            foreach (double[] p in dome.VertexVectorsSphere)
            {
                Vector3d vec = new Vector3d(p[0], p[1], p[2]);
                vec = Vector3d.Multiply(vec_sp_len, vec);
                meshObstructed.Vertices.Add(vec + sp);
            }
            foreach (int[] f in dome.Faces)
            {
                meshObstructed.Faces.AddFace(f[0], f[1], f[2]);
            }
            meshObstructed.UnifyNormals();

            if (drawviewfactors)
            {
                List <Vector3d> vec_sky_list = new List <Vector3d>();
                List <int>      vec_int      = new List <int>();
                for (int i = 0; i < meshObstructed.Vertices.Count; i++)
                {
                    Vector3d testvec = meshObstructed.Vertices[i] - sp;
                    if (testvec.Z >= 0.0)
                    {
                        vec_sky_list.Add(testvec);
                        vec_int.Add(i);
                    }
                }
                Color[] colors = new Color[meshObstructed.Vertices.Count];
                for (int i = 0; i < meshObstructed.Vertices.Count; i++)
                {
                    colors[i] = Color.FromArgb(100, 255, 255, 255);  //alpha not working
                }
                meshObstructed.VertexColors.SetColors(colors);
                Vector3d[] vec_sky = vec_sky_list.ToArray();
                bool[]     shadow  = new bool[vec_sky_list.Count];
                if (context.Count > 0)
                {
                    CShadow.CalcShadowMT(sp, new Vector3d(0, 0, 1), 0.001, vec_sky, context.ToArray(), ref shadow, paropts);
                }

                int j = 0;
                foreach (int i in vec_int)
                {
                    Color c = new Color();
                    if (shadow[j])
                    {
                        // Custom material, DisplayMaterial (rhinostyle) rendering material. and make in override DrawViewportMesh
                        c = Color.FromArgb(100, 0, 0, 0);   //alpha not working
                        meshObstructed.VertexColors.SetColor(i, c);
                        _vectorsObstructed.Add(i);
                    }
                    j++;
                }
            }
            else if (drawcumskymatrix)
            {
                // https://www.sciencedirect.com/science/article/pii/S0038092X04001161
                // http://alexandria.tue.nl/openaccess/635611/p1153final.pdf
                // Solarmodel.dll needs new function to compute cumulative sky view matrix (requires obstruction check from drawviewfactors
                // 1. calc perez diffuse for each hour. use that value (hor, circum, dome) and assign it to each mesh face
                // 2. DNI is computed directly onto SP
                // 3. visualize colored dome for diff only.
                // 4. add text to sensorpoint, stating annual irradiation (DNI plus diff)
                //
                // cumskymatrix seperate component!! coz it can be re-used for several sensor points
                // matrix inversion as in robinson stone to compute irradiation on all sensor points with refl.?
                //
                // needs a separate component that uses cumskymatrix on a number of SPs and visualizes that analysis surface.
                //... or use this component, output the sensorpoints, give it to a surface and make surface evaluate with the points, and recolor that surface

                if (dni.Count == 8760 && dhi.Count == 8760)     // only continue, if solar irradiance time series are provided
                {
                    //Rhino.RhinoApp.WriteLine("Leggo!");
                    //Context.cWeatherdata weather;
                    //weather.DHI = dhi;
                    //weather.DNI = dni;
                    //weather.Snow = new List<double>();
                    //double[] beta = new double[1] { beta_in };
                    //double[] psi = new double[1] { psi_in };
                    //Sensorpoints.p3d[] coord = new Sensorpoints.p3d[1];   //dummy variables. will not be used in this simplified simulation
                    //coord[0].X = 0;
                    //coord[0].Y = 0;
                    //coord[0].Z = 0;
                    //Sensorpoints.v3d[] normal = new Sensorpoints.v3d[1];   //dummy variables. will not be used in this simplified simulation
                    //normal[0].X = 0;
                    //normal[0].Y = 1;
                    //normal[0].Z = 0;


                    //double[] albedo = new double[8760];
                    //for (int t = 0; t < 8760; t++)
                    //{
                    //    albedo[t] = albedo1;
                    //}

                    //Console.WriteLine("Calculating irradiation...");
                    //Sensorpoints p = new Sensorpoints(beta, psi, coord, normal, reclvl);
                    //p.SetSimpleSkyMT(beta, paropts);
                    //p.SetSimpleGroundReflectionMT(beta, albedo, weather, sunvectors.ToArray(), paropts);
                    //p.CalcIrradiationMT(weather, sunvectors.ToArray(), paropts);

                    // hold on... i need a new function in SolarModel for CumSkyMatrix
                }
                else
                {
                    Rhino.RhinoApp.WriteLine("No data for Direct Normal Irradiance and Diffuse Horizontal Irradiance provided... Please provide 8760 time series for each.");
                }
            }

            if (drawviewfactors || drawcumskymatrix)
            {
                //_colouredMesh.Add(meshObstructed);
                _viewFactors = meshObstructed;
            }

            //////////////////////////////////////////////////////////////////////////////////////////
            /// Solar Vectors
            List <Sphere>    spheres  = new List <Sphere>();
            double           fontsize = vec_sp_len / 50.0;
            List <SunVector> sunvectors_list;

            SunVector.Create8760SunVectors(out sunvectors_list, longitude, latitude, year);
            int count = 0;

            if (draw_solarvec)
            {
                foreach (int h in hoy)
                {
                    Vector3d vec = new Vector3d(sunvectors_list[h].udtCoordXYZ.x, sunvectors_list[h].udtCoordXYZ.y, sunvectors_list[h].udtCoordXYZ.z);
                    vec = Vector3d.Multiply(vec_sp_len, vec);
                    Point3d solarpoint = new Point3d(Point3d.Add(sp, vec));
                    Line    ln         = new Line(sp, solarpoint);
                    ln.Flip();
                    _solar_vectors.Add(ln);
                    if (sunvectors_list[h].udtCoordXYZ.z < 0)
                    {
                        _night_time.Add(true);
                    }
                    else
                    {
                        _night_time.Add(false);
                    }

                    int    year_now  = sunvectors_list[h].udtTime.iYear;
                    int    month_now = sunvectors_list[h].udtTime.iMonth;
                    int    day_now   = sunvectors_list[h].udtTime.iDay;
                    double hour_now  = sunvectors_list[h].udtTime.dHours;
                    string hour_now2 = Convert.ToString(hour_now);
                    if (hour_now < 10)
                    {
                        hour_now2 = "0" + Convert.ToString(hour_now);
                    }
                    string strval = Convert.ToString(year_now) + "/ " + Convert.ToString(month_now) + "/ " + Convert.ToString(day_now) + "/ " + hour_now2 + ":00";
                    Plane  pl     = new Plane(ln.From, new Vector3d(-1, 0, 0));
                    //Plane pl = new Plane(ln.From, vec);
                    var te = Rhino.RhinoDoc.ActiveDoc.Objects.AddText(strval, pl, fontsize, "Baskerville", false, false);
                    Rhino.DocObjects.TextObject txt = Rhino.RhinoDoc.ActiveDoc.Objects.Find(te) as Rhino.DocObjects.TextObject;
                    _txt.Add(new List <Curve>());
                    if (txt != null)
                    {
                        var     tt = txt.Geometry as Rhino.Geometry.TextEntity;
                        Curve[] A  = tt.Explode();

                        foreach (Curve crv in A)
                        {
                            _txt[count].Add(crv);
                        }
                    }
                    count++;
                    Rhino.RhinoDoc.ActiveDoc.Objects.Delete(te, true);

                    Sphere sph = new Sphere(ln.From, vec_sp_len / 30.0);
                    spheres.Add(sph);
                }
            }


            //////////////////////////////////////////////////////////////////////////////////////////
            /// SUN PATH
            /// !!! wierd sun paths at extreme longitudes -> time shift... +/- UCT
            // draw solar paths: curves that connect each month, but for the same hour
            if (draw_sunpath)
            {
                for (int hod = 0; hod < 24; hod++)
                {
                    List <Point3d> pts = new List <Point3d>();
                    for (int d = 0; d < 365; d++)
                    {
                        int      h   = hod + 24 * d;
                        Vector3d vec = new Vector3d(sunvectors_list[h].udtCoordXYZ.x, sunvectors_list[h].udtCoordXYZ.y, sunvectors_list[h].udtCoordXYZ.z);
                        vec = Vector3d.Multiply(vec_sp_len, vec);
                        if (vec.Z > 0)
                        {
                            Point3d solarpoint = new Point3d(Point3d.Add(sp, vec));
                            pts.Add(solarpoint);
                        }
                    }
                    if (pts.Count > 0)
                    {
                        PolylineCurve crv = new PolylineCurve(pts);
                        _sun_paths.Add(crv);
                    }
                }

                // draw solar paths; curves that connects each hour, but for the same month
                int interv = 365 / 12;
                for (int m = 0; m < 12; m++)
                {
                    List <Point3d> pts = new List <Point3d>();
                    for (int hod = 0; hod < 24; hod++)
                    {
                        int      h   = hod + ((m * interv + interv / 2) * 24);
                        Vector3d vec = new Vector3d(sunvectors_list[h].udtCoordXYZ.x, sunvectors_list[h].udtCoordXYZ.y, sunvectors_list[h].udtCoordXYZ.z);
                        vec = Vector3d.Multiply(vec_sp_len, vec);
                        if (vec.Z > 0)
                        {
                            Point3d solarpoint = new Point3d(Point3d.Add(sp, vec));
                            pts.Add(solarpoint);
                        }
                    }
                    if (pts.Count > 0)
                    {
                        PolylineCurve crv = new PolylineCurve(pts);
                        _sun_paths.Add(crv);
                    }
                }
            }


            //////////////////////////////////////////////////////////////////////////////////////////
            /// OUTPUT
            DA.SetData(0, _viewFactors);        // this mesh needs to be colored according to view factor or cumulative sky matrix
            DA.SetDataList(1, _solar_vectors);
            DA.SetDataList(2, _sun_paths);
            DA.SetDataList(3, spheres);
        }
Ejemplo n.º 2
0
    internal static RhinoObject CreateRhinoObjectHelper(IntPtr pRhinoObject)
    {
      if (IntPtr.Zero == pRhinoObject)
        return null;

      uint sn = UnsafeNativeMethods.CRhinoObject_RuntimeSN(pRhinoObject);
      if (sn < 1)
        return null;

      int type = UnsafeNativeMethods.CRhinoRhinoObject_GetRhinoObjectType(pRhinoObject);
      if (type < 0)
        return null;
      RhinoObject rc;
      switch (type)
      {
        case idxCRhinoPointObject: //1
          rc = new PointObject(sn);
          break;
        case idxCRhinoCurveObject: //2
          rc = new CurveObject(sn);
          break;
        case idxCRhinoMeshObject: //3
          rc = new MeshObject(sn);
          break;
        case idxCRhinoBrepObject: //4
          rc = new BrepObject(sn);
          break;
        case idxCRhinoPointCloudObject: //5
          rc = new PointCloudObject(sn);
          break;
        case idxCRhinoAnnotationTextObject: //6
          rc = new TextObject(sn);
          break;
        case idxCRhinoSurfaceObject: //7
          rc = new SurfaceObject(sn);
          break;
        case idxCRhinoInstanceObject: //8
          rc = new InstanceObject(sn);
          break;
        case idxCRhinoHatchObject: //9
          rc = new HatchObject(sn);
          break;
        case idxCRhinoDetailViewObject: //10
          rc = new DetailViewObject(sn);
          break;
        case idxCRhinoClippingPlaneObject: //11
          rc = new ClippingPlaneObject(sn);
          break;
        case idxCRhinoTextDot: //12
          rc = new TextDotObject(sn);
          break;
        case idxCRhinoGripObject: //13
          rc = new GripObject(sn);
          break;
#if USING_V5_SDK
        case idxCRhinoExtrusionObject: //14
          rc = new ExtrusionObject(sn);
          break;
#endif
        case idxCRhinoLinearDimension: //15
          rc = new LinearDimensionObject(sn);
          break;
        case idxCRhinoAnnotationObject: //16
          rc = new AnnotationObjectBase(sn);
          break;
        case idxCRhinoLight: //17
          rc = new LightObject(sn);
          break;
        case idxCRhinoMorphControl: //18
          rc = new MorphControlObject(sn);
          break;
        case idxCRhinoRadialDimension: //19
          rc = new RadialDimensionObject(sn);
          break;
        case idxCRhinoAngularDimension: //20
          rc = new AngularDimensionObject(sn);
          break;
        default:
          rc = new RhinoObject(sn);
          break;
      }
      return rc;
    }