Exemple #1
0
void MeshPartition::setVolume(QString volume_name)
{
  m_Grid = GuiMainWindow::pointer()->getGrid();
  resetOrientation(m_Grid);
  VolumeDefinition V = GuiMainWindow::pointer()->getVol(volume_name);
  QList<vtkIdType> cls;
  EG_VTKDCC(vtkIntArray, cell_code,   m_Grid, "cell_code");
  EG_VTKDCC(vtkIntArray, cell_orgdir, m_Grid, "cell_orgdir");
  EG_VTKDCC(vtkIntArray, cell_curdir, m_Grid, "cell_curdir");
  EG_VTKDCC(vtkIntArray, cell_voldir, m_Grid, "cell_voldir");
  for (vtkIdType id_cell = 0; id_cell < m_Grid->GetNumberOfCells(); ++id_cell) {
    if (isSurface(id_cell, m_Grid)) {
      int bc = cell_code->GetValue(id_cell);
      cell_voldir->SetValue(id_cell, 0);
      if (V.getSign(bc) != 0) {
        cls.append(id_cell);
        if (V.getSign(bc) == -1) {
          cell_voldir->SetValue(id_cell, 1);
        }
      }
    } else {
      if (cell_code->GetValue(id_cell) == V.getVC()) {
        cls.append(id_cell);
      }
    }
  }
  setCells(cls);
}
Exemple #2
0
void MeshPartition::setOriginalOrientation()
{
  EG_VTKDCC(vtkIntArray, cell_curdir, m_Grid, "cell_curdir");
  EG_VTKDCC(vtkIntArray, cell_orgdir, m_Grid, "cell_orgdir");
  foreach (vtkIdType id_cell, m_Cells) {
    if (isSurface(id_cell, m_Grid)) {
      if (cell_curdir->GetValue(id_cell) != cell_orgdir->GetValue(id_cell)) {
        reorientateFace(m_Grid, id_cell);
      }
    }
  }
}
Exemple #3
0
void Su2Writer::writeBoundaries()
{
  QTextStream f(m_File);
  f << "%\n";
  f << "% Boundary elements\n";
  f << "%\n";
  QSet<int> bcs = GuiMainWindow::pointer()->getAllBoundaryCodes();
  f << "NMARK= " << bcs.size() << "\n";
  EG_VTKDCC(vtkIntArray, cell_code, m_Grid, "cell_code");
  foreach (int bc, bcs) {
    BoundaryCondition BC = GuiMainWindow::pointer()->getBC(bc);
    f << "MARKER_TAG= " << BC.getName() << "\n";
    QList<vtkIdType> faces;
    for (vtkIdType id_cell = 0; id_cell < m_Grid->GetNumberOfCells(); ++id_cell) {
      if (isSurface(id_cell, m_Grid)) {
        if (cell_code->GetValue(id_cell) == bc) {
          faces.append(id_cell);
        }
      }
    }
    f << "MARKER_ELEMS= " << faces.size() << "\n";
    foreach (vtkIdType id_cell, faces) {
      f << m_Grid->GetCellType(id_cell);
      vtkIdType N_pts, *pts;
      m_Grid->GetCellPoints(id_cell, N_pts, pts);
      for (int j = 0; j < N_pts; ++j) {
        f << " " << pts[j];
      }
      f << "\n";
    }
void BoundaryLayerOperation::readSettings()
{
  EG_VTKDCC(vtkIntArray, cell_code, m_Grid, "cell_code");
  QString buffer = GuiMainWindow::pointer()->getXmlSection("engrid/blayer/settings");
  if(!buffer.isEmpty()) {
    QTextStream in(&buffer, QIODevice::ReadOnly);
    int num_bcs;
    in >> num_bcs;
    QVector<bool> use_bc(num_bcs);
    int N = 0;
    for (int i = 0; i < num_bcs; ++i) {
      int state;
      in >> state;
      use_bc[i] = bool(state);
      if (use_bc[i]) {
        ++N;
      }
    }
    m_BoundaryLayerCodes.resize(N);
    QVector<int> bcs;
    mainWindow()->getAllBoundaryCodes(bcs);
    int j = 0;
    for (int i = 0; i < bcs.size(); ++i) {
      if (use_bc[i]) {
        m_BoundaryLayerCodes[j++] = bcs[i];
      }
    }
    m_LayerAdjacentBoundaryCodes.clear();
    for (vtkIdType id_cell = 0; id_cell < m_Grid->GetNumberOfCells(); ++id_cell) {
      if (isSurface(id_cell, m_Grid)) {
        if (m_BoundaryLayerCodes.contains(cell_code->GetValue(id_cell))) {
          for (int i = 0; i < m_Part.c2cGSize(id_cell); ++i) {
            vtkIdType id_neigh = m_Part.c2cGG(id_cell, i);
            if (!m_BoundaryLayerCodes.contains(cell_code->GetValue(id_neigh))) {
              m_LayerAdjacentBoundaryCodes.insert(cell_code->GetValue(id_neigh));
            }
          }
        }
      }
    }
    in >> m_FeatureAngle;
    m_FeatureAngle = deg2rad(m_FeatureAngle);
    in >> m_StretchingRatio;
    in >> m_FarfieldRatio;
    in >> m_NumBoundaryLayerVectorRelaxations;
    in >> m_NumBoundaryLayerHeightRelaxations;
    in >> m_FaceSizeLowerLimit;
    in >> m_FaceSizeUpperLimit;
    in >> m_FaceAngleLimit;
    m_FaceAngleLimit = deg2rad(m_FaceAngleLimit);
    in >> m_MaxHeightInGaps;
    in >> m_RadarAngle;
    int use_grouping;
    in >> use_grouping;
    m_UseGrouping = use_grouping;
    in >> m_GroupingAngle;
    m_GroupingAngle = deg2rad(m_GroupingAngle);
  }
Exemple #5
0
void BrlcadReader::findBoundaryCodes()
{
  int num_grids = m_Grids.size();
  QVector<vtkUnstructuredGrid*> grids(num_grids);
  qCopy(m_Grids.begin(), m_Grids.end(), grids.begin());
  QVector<FaceFinder> finders(num_grids);
  for (int i_grid = 0; i_grid < num_grids; ++i_grid) {
    finders[i_grid].setGrid(grids[i_grid]);
  }
  EG_VTKDCC(vtkIntArray, cell_code, m_Grid, "cell_code");
  bool has_errors = false;
  QVector<bool> bc_exists(num_grids, false);
  for (vtkIdType id_cell = 0; id_cell < m_Grid->GetNumberOfCells(); ++id_cell) {
    int best_grid = -1;
    double L_min = 1e99;
    vec3_t x1 = cellCentre(m_Grid, id_cell);
    for (int i_grid = 0; i_grid < num_grids; ++i_grid) {
      double L = 1e99;
      vtkIdType id_closest = finders[i_grid].getClosestFace(x1, L);
      vec3_t x2(-999,-999,-999);
      if (id_closest != -1) x2 = cellCentre(m_Grids[i_grid], id_closest);
      if (id_closest != -1) {
        if (L < L_min) {
          best_grid = i_grid;
          L_min = L;
        }
      }
    }
    if (best_grid == -1) {
      has_errors = true;
      cell_code->SetValue(id_cell, 9999);
    } else {
      bc_exists[best_grid] = true;
      cell_code->SetValue(id_cell, best_grid + 1);
    }
  }
  GuiMainWindow::pointer()->clearBCs();
  int bc_max = 1;
  QVector<int> bc_map(num_grids+1,9999);
  m_BC2GridIndex.clear();
  for (int i_grid = 0; i_grid < num_grids; ++i_grid) {
    if (bc_exists[i_grid]) {
      bc_map[i_grid+1] = bc_max;
      GuiMainWindow::pointer()->addBC(bc_max, BoundaryCondition(m_BCNames[grids[i_grid]], "patch"));
      m_BC2GridIndex[bc_max] = i_grid;
      ++bc_max;
    }
  }
  for (vtkIdType id_cell = 0; id_cell < m_Grid->GetNumberOfCells(); ++id_cell) {
    if (cell_code->GetValue(id_cell) != 9999) {
      cell_code->SetValue(id_cell, bc_map[cell_code->GetValue(id_cell)]);
    }
  }
  if (has_errors) {
    GuiMainWindow::pointer()->addBC(9999, BoundaryCondition("error-faces", "patch"));
  }
  GuiMainWindow::pointer()->updateBoundaryCodes(true);
}
Exemple #6
0
void MeshQuality::computeNodesFromCells()
{
  EG_VTKDCN(vtkDoubleArray, node_mesh_quality, m_Grid, "node_mesh_quality");
  EG_VTKDCC(vtkDoubleArray, cell_mesh_quality, m_Grid, "cell_mesh_quality");
  EG_FORALL_NODES(id_node, m_Grid) {
    double mq = 1;
    for (int i = 0; i < m_Part.n2cGSize(id_node); ++i) {
      vtkIdType id_cell = m_Part.n2cGG(id_node, i);
      mq = min(cell_mesh_quality->GetValue(id_cell), mq);
    }
    node_mesh_quality->SetValue(id_node, mq);
  }
Exemple #7
0
void NeutralWriter::operate()
{
  try {
    QFileInfo file_info(GuiMainWindow::pointer()->getFilename());
    readOutputFileName(file_info.completeBaseName() + ".mesh");
    if (isValid()) {
      QFile file(getFileName());
      file.open(QIODevice::WriteOnly | QIODevice::Text);
      QTextStream f(&file);
      f << m_Grid->GetNumberOfPoints() << "\n";
      for (vtkIdType pointId = 0; pointId < m_Grid->GetNumberOfPoints(); ++pointId) {
        vec3_t x;
        m_Grid->GetPoints()->GetPoint(pointId, x.data());
        f << x[0] << " " << x[1] << " " << x[2] << "\n";
      };
      vtkIdType Nvol = 0;
      vtkIdType Nsurf = 0;
      for (vtkIdType cellId = 0; cellId < m_Grid->GetNumberOfCells(); ++cellId) {
        vtkIdType cellType = m_Grid->GetCellType(cellId);
        if ((cellType != VTK_TRIANGLE) && (cellType != VTK_TETRA)) {
          EG_ERR_RETURN("only simplex elements are allowed for the NEUTRAL format");
        };
        if (isSurface(cellId, m_Grid)) {
          ++Nsurf;
        } else {
          ++Nvol;
        };
      };
      f << Nvol << "\n";
      for (vtkIdType cellId = 0; cellId < m_Grid->GetNumberOfCells(); ++cellId) {
        if (!isSurface(cellId, m_Grid)) {
          vtkIdType Npts, *pts;
          m_Grid->GetCellPoints(cellId, Npts, pts);
          f << "1 " << pts[0]+1 << " " << pts[1]+1 << " " << pts[3]+1 << " " << pts[2]+1 << "\n";
        };
      };
      f << Nsurf << "\n";
      EG_VTKDCC(vtkIntArray, cell_code, m_Grid, "cell_code");
      for (vtkIdType cellId = 0; cellId < m_Grid->GetNumberOfCells(); ++cellId) {
        if (isSurface(cellId, m_Grid)) {
          vtkIdType Npts, *pts;
          m_Grid->GetCellPoints(cellId, Npts, pts);
          f << 1;//cell_code->GetValue(cellId);
          f << " " << pts[2]+1 << " " << pts[1]+1 << " " << pts[0]+1 << "\n";
        };
      };
    };
  } catch (Error err) {
    err.display();
  };
};
Exemple #8
0
QString DrNumWriter::boundaryCode(vtkIdType id_cell, int i)
{
  EG_VTKDCC(vtkIntArray, cell_code, m_Grid, "cell_code");
  QString bcode = "0";
  vtkIdType id_neigh = m_Part.c2cGG(id_cell, i);
  if (id_neigh != -1) {
    if (m_CellToCartPatch[id_neigh] == -1) {
      if (isSurface(id_neigh, m_Grid)) {
        BoundaryCondition bc = GuiMainWindow::pointer()->getBC(cell_code->GetValue(id_neigh));
        if (bc.getName() != "unknown") {
          bcode = bc.getType();
        }
      }
    }
  }
  return bcode;
}
void UpdateDesiredMeshDensity::computeExistingLengths()
{
  QSet<int> all_bcs = GuiMainWindow::pointer()->getAllBoundaryCodes();
  QSet<int> fixed_bcs = all_bcs - m_BoundaryCodes;
  QVector<double> edge_length(m_Grid->GetNumberOfPoints(), 1e99);
  QVector<int> edge_count(m_Grid->GetNumberOfPoints(), 0);
  m_Fixed.fill(false, m_Grid->GetNumberOfPoints());
  EG_VTKDCC(vtkIntArray, cell_code, m_Grid, "cell_code");
  for (vtkIdType id_cell = 0; id_cell < m_Grid->GetNumberOfCells(); ++id_cell) {
    if (isSurface(id_cell, m_Grid)) {
      if (fixed_bcs.contains(cell_code->GetValue(id_cell))) {
        vtkIdType N_pts, *pts;
        m_Grid->GetCellPoints(id_cell, N_pts, pts);
        QVector<vec3_t> x(N_pts);
        for (int i = 0; i < N_pts; ++i) {
          m_Grid->GetPoint(pts[i], x[i].data());
          m_Fixed[pts[i]] = true;
        }
        for (int i = 0; i < N_pts; ++i) {
          int j = i + 1;
          if (j >= N_pts) {
            j = 0;
          }
          double L = (x[i] - x[j]).abs();
          edge_length[pts[i]] = min(edge_length[pts[i]], L);
          edge_length[pts[j]] = min(edge_length[pts[j]], L);
          ++edge_count[pts[i]];
          ++edge_count[pts[j]];
        }
      }
    }
  }
  EG_VTKDCN(vtkDoubleArray, characteristic_length_desired,   m_Grid, "node_meshdensity_desired");
  for (vtkIdType id_node = 0; id_node < m_Grid->GetNumberOfPoints(); ++id_node) {
    if (edge_count[id_node] > 0) {
      if (edge_length[id_node] > 1e98) {
        EG_BUG;
      }
      characteristic_length_desired->SetValue(id_node, edge_length[id_node]);
    }
  }
}
Exemple #10
0
QList<vtkIdType> StitchHoles::getNextHole()
{
  QList<vtkIdType> loop_nodes;
  EG_VTKDCC(vtkIntArray, cell_code, m_Grid, "cell_code");

  // find the first two nodes of the hole
  //
  vtkIdType id_node1 = -1;
  vtkIdType id_node2 = -1;
  EG_FORALL_CELLS (id_cell, m_Grid) {
    if (isSurface(id_cell, m_Grid) && cell_code->GetValue(id_cell) == m_Bc) {
      for (int i = 0; i < m_Part.c2cGSize(id_cell); ++i) {
        if (m_Part.c2cGG(id_cell, i) == -1) {
          QList<vtkIdType> pts;
          getPointsOfCell(m_Grid, id_cell, pts);
          pts << pts.first();
          id_node1 = pts[i+1];
          id_node2 = pts[i];
          break;
        }
      }
      if (id_node1 != -1) {
        break;
      }
    }
  }
  if (id_node1 == -1) {
    return loop_nodes;
  }

  // create node loop around hole
  //
  vtkIdType id_start = id_node1;
  loop_nodes << id_node1;
  vec3_t x0;
  m_Grid->GetPoint(id_node1, x0.data());
  while (id_node2 != id_start && loop_nodes.size() < m_Grid->GetNumberOfPoints()) {
    loop_nodes << id_node2;
    vec3_t x;
    m_Grid->GetPoint(id_node2, x.data());
    x0 += x;
    bool found = false;
    for (int i = 0; i < m_Part.n2nGSize(id_node2); ++i) {
      vtkIdType id_node3 = m_Part.n2nGG(id_node2, i);
      if (id_node3 != id_node1) {
        QList<vtkIdType> edge_faces;
        m_Part.getEdgeFaces(id_node2, id_node3, edge_faces);
        if (edge_faces.size() == 1) {
          found = true;
          id_node1 = id_node2;
          id_node2 = id_node3;
          break;
        }
      }
    }
    if (!found) {
      loop_nodes.clear();
      return loop_nodes;
    }
  }
  x0 *= 1.0/loop_nodes.size();
  m_Cad->snap(x0);
  vec3_t n = m_Cad->getLastNormal();
  setOrigin(x0);
  setNormal(n);
  setupTransformation();
  return loop_nodes;
}
void RestrictToAvailableVolumeCells::operate()
{
  QList<vtkIdType> vol_cells, surf_cells;
  for (vtkIdType id_cell = 0; id_cell < m_Grid->GetNumberOfCells(); ++id_cell) {
    if (isVolume(id_cell, m_Grid)) {
      vol_cells.append(id_cell);
    }
  }
  foreach (vtkIdType id_cell, vol_cells) {
    for (int i = 0; i < m_Part.c2cGSize(id_cell); ++i) {
      vtkIdType id_neigh = m_Part.c2cGG(id_cell, i);
      if (id_neigh >= 0) {
        if (isSurface(id_neigh, m_Grid)) {
          surf_cells.append(id_neigh);
        }
      }
    }
  }
  MeshPartition vol_part(m_Grid);
  vol_part.setCells(vol_cells + surf_cells);
  EG_VTKSP(vtkUnstructuredGrid, new_grid1);
  vol_part.extractToVtkGrid(new_grid1);
  MeshPartition new_part1(new_grid1, true);
  QList<QVector<vtkIdType> > new_faces;
  for (vtkIdType id_cell = 0; id_cell < new_grid1->GetNumberOfCells(); ++id_cell) {
    if (isVolume(id_cell, new_grid1)) {
      for (int i = 0; i < new_part1.c2cGSize(id_cell); ++i) {
        vtkIdType id_neigh = new_part1.c2cGG(id_cell, i);
        if (id_neigh == -1) {
          QVector<vtkIdType> pts;
          getFaceOfCell(new_grid1, id_cell, i, pts);
          new_faces.append(pts);
        }
      }
    }
  }
  EG_VTKSP(vtkUnstructuredGrid, new_grid2);
  allocateGrid(new_grid2, new_grid1->GetNumberOfCells() + new_faces.size(), new_grid1->GetNumberOfPoints());
  EG_VTKDCC(vtkIntArray, cell_code1, new_grid1, "cell_code");
  EG_VTKDCC(vtkIntArray, cell_code2, new_grid2, "cell_code");
  for (vtkIdType id_node = 0; id_node < new_grid1->GetNumberOfPoints(); ++id_node) {
    vec3_t x;
    new_grid1->GetPoint(id_node, x.data());
    new_grid2->GetPoints()->SetPoint(id_node, x.data());
    copyNodeData(new_grid1, id_node, new_grid2, id_node);
  }
  int bc_new = 0;
  for (vtkIdType id_cell = 0; id_cell < new_grid1->GetNumberOfCells(); ++id_cell) {
    copyCell(new_grid1, id_cell, new_grid2);
    copyCellData(new_grid1, id_cell, new_grid2, id_cell);
    if (isSurface(id_cell, new_grid1)) {
      bc_new = max(bc_new, cell_code1->GetValue(id_cell) + 1);
    } else {
      cell_code2->SetValue(id_cell, 0);
    }
  }
  foreach (QVector<vtkIdType> face, new_faces) {
    vtkIdType type = VTK_POLYGON;
    if (face.size() == 3) {
      type = VTK_TRIANGLE;
    } else if (face.size() == 4) {
      type = VTK_QUAD;
    }
    vtkIdType id_cell = new_grid2->InsertNextCell(type, face.size(), face.data());
    cell_code2->SetValue(id_cell, bc_new);
  }
void CreateCadTesselation::scan(bool create_grid, int interlaces)
{
  m_Dx = (m_X2[0] - m_X1[0])/(m_Ni-1);
  m_Dy = (m_X2[1] - m_X1[1])/(m_Nj-1);
  m_Dz = (m_X2[2] - m_X1[2])/(m_Nk-1);

  EG_VTKSP(vtkImageData, gdata);
  gdata->SetDimensions(m_Ni, m_Nj, m_Nk);
  EG_VTKSP(vtkFloatArray, g);
  g->SetName("g");
  g->SetNumberOfValues(m_Ni*m_Nj*m_Nk);
  gdata->GetPointData()->AddArray(g);
  for (int i = 0; i < m_Ni; ++i) {
    for (int j = 0; j < m_Nj; ++j) {
      for (int k = 0; k < m_Nk; ++k) {                
        if (preserveFluid()) {
          g->SetValue(getIdx(i,j,k), 1);
        } else {
          g->SetValue(getIdx(i,j,k), 0);
        }
      }
    }
  }
  m_XScan1 = m_X2;
  m_XScan2 = m_X1;

  vec3_t x_in, x_out, n_in, n_out;

  int progress = 0;
  m_GeometryFound = false;

  double dxi = m_Dx/(interlaces + 1);
  double dyi = m_Dy/(interlaces + 1);
  double dzi = m_Dz/(interlaces + 1);

  double shift = 1e-6;

  // scan ij plane -> k direction
  //      xy plane -> z direction
  for (int i = 0; i < m_Ni; ++i) {
    for (int j = 0; j < m_Nj; ++j) {
      vec3_t x0 = getX(i,j,0);
      for (int i_il = 0; i_il <= interlaces; ++i_il) {
        for (int j_il = 0; j_il <= interlaces; ++j_il) {
          vec3_t x = x0;
          x[0] += i_il*dxi;
          x[1] += j_il*dyi;
          int k_last = 0;
          while (shootRay(x, vec3_t(0,0,1), x_in, x_out, n_in, n_out)) {
            m_GeometryFound = true;
            m_XScan1[2] = min(m_XScan1[2], x_in[2]);
            m_XScan2[2] = max(m_XScan2[2], x_out[2]);
            int k1 = int((x_in[2] - m_X1[2])/m_Dz) + 1;
            int k2 = int((x_out[2] - m_X1[2])/m_Dz);
            if (preserveFluid()) {
              for (int k = k_last; k < min(m_Nk-1,k1); ++k) {
                g->SetValue(getIdx(i,j,k), 0);
              }
            } else {
              for (int k = max(0,k1); k <= min(m_Nk-1,k2); ++k) {
                g->SetValue(getIdx(i,j,k), 1);
              }
            }
            x = x_out;
            x[2] += shift*m_Dz;
            k_last = min(m_Nk-1,k2+1);
          }
          if (preserveFluid()) {
            for (int k = k_last; k <= m_Nk-1; ++k) {
              g->SetValue(getIdx(i,j,k), 0);
            }
          }
        }
      }
    }
    progress += m_Nj;
    GuiMainWindow::pointer()->setProgress(progress);
  }

  // scan ik plane -> j direction
  //      xz plane -> y direction
  for (int i = 0; i < m_Ni; ++i) {
    for (int k = 0; k < m_Nk; ++k) {
      vec3_t x0 = getX(i,0,k);
      for (int i_il = 0; i_il <= interlaces; ++i_il) {
        for (int k_il = 0; k_il <= interlaces; ++k_il) {
          vec3_t x = x0;
          x[0] += i_il*dxi;
          x[2] += k_il*dzi;
          int j_last = 0;
          /*
          if (fabs(x0[0]) < 1.5 && fabs(x0[2]) < 1.5 && create_grid) {
            cout << x0 << endl;
          }
          */
          while (shootRay(x, vec3_t(0,1,0), x_in, x_out, n_in, n_out)) {
            m_GeometryFound = true;
            m_XScan1[1] = min(m_XScan1[1], x_in[1]);
            m_XScan2[1] = max(m_XScan2[1], x_out[1]);
            int j1 = int((x_in[1]-m_X1[1])/m_Dy) + 1;
            int j2 = int((x_out[1]-m_X1[1])/m_Dy);
            if (preserveFluid()) {
              for (int j = j_last; j < min(m_Nj-1,j1); ++j) {
                g->SetValue(getIdx(i,j,k), 0);
              }
            } else {
              for (int j = max(0,j1); j <= min(m_Nj-1,j2); ++j) {
                g->SetValue(getIdx(i,j,k), 1);
              }
            }
            x = x_out;
            x[1] += shift*m_Dy;
            j_last = min(m_Nj-1,j2+1);
          }
          if (preserveFluid()) {
            for (int j = j_last; j <= m_Nj-1; ++j) {
              g->SetValue(getIdx(i,j,k), 0);
            }
          }
        }
      }
    }
    progress += m_Nk;
    GuiMainWindow::pointer()->setProgress(progress);
  }

  // scan jk plane -> i direction
  //      yz plane -> x direction
  for (int j = 0; j < m_Nj; ++j) {
    for (int k = 0; k < m_Nk; ++k) {
      vec3_t x0 = getX(0,j,k);
      for (int j_il = 0; j_il <= interlaces; ++j_il) {
        for (int k_il = 0; k_il <= interlaces; ++k_il) {
          vec3_t x = x0;
          x[1] += j_il*dyi;
          x[2] += k_il*dzi;
          int i_last = 0;
          while (shootRay(x, vec3_t(1,0,0), x_in, x_out, n_in, n_out)) {
            m_GeometryFound = true;
            m_XScan1[0] = min(m_XScan1[0], x_in[0]);
            m_XScan2[0] = max(m_XScan2[0], x_out[0]);
            int i1 = int((x_in[0]-m_X1[0])/m_Dx) + 1;
            int i2 = int((x_out[0]-m_X1[0])/m_Dx);
            if (preserveFluid()) {
              for (int i = i_last; i < min(m_Ni-1,i1); ++i) {
                g->SetValue(getIdx(i,j,k), 0);
              }
            } else {
              for (int i = max(0,i1); i <= min(m_Ni-1,i2); ++i) {
                g->SetValue(getIdx(i,j,k), 1);
              }
            }
            x = x_out;
            x[0] += shift*m_Dx;
            i_last = min(m_Ni-1,i2+1);
          }
          if (preserveFluid()) {
            for (int i = i_last; i <= m_Ni-1; ++i) {
              g->SetValue(getIdx(i,j,k), 0);
            }
          }
        }
      }
    }
    progress += m_Nk;
    GuiMainWindow::pointer()->setProgress(progress);
  }

  if (create_grid) {

    GuiMainWindow::pointer()->resetProgress("smoothing", m_Ni*m_Nj*m_Nk*m_NumIterations);

    // smooth image data
    int progress = 0;
    for (int iter = 1; iter <= m_NumIterations; ++iter) {
      for (int i = 1; i < m_Ni-1; ++i) {
        for (int j = 1; j < m_Nj-1; ++j) {
          for (int k = 1; k < m_Nk-1; ++k) {
            int idx = getIdx(i,j,k);
            bool preserve = false;
            if (m_PreservationType == 1 && g->GetValue(idx) > 0.999) {
              preserve = true;
            }
            if (m_PreservationType == 2 && g->GetValue(idx) < 0.001) {
              preserve = true;
            }
            if (!preserve) {
              double g_new = 0;
              g_new += g->GetValue(getIdx(i+1,j,k));
              g_new += g->GetValue(getIdx(i-1,j,k));
              g_new += g->GetValue(getIdx(i,j+1,k));
              g_new += g->GetValue(getIdx(i,j-1,k));
              g_new += g->GetValue(getIdx(i,j,k+1));
              g_new += g->GetValue(getIdx(i,j,k-1));
              g_new *= 1.0/6.0;
              g->SetValue(idx, g_new);
            }
          }
        }
        progress += m_Nj*m_Nk;
        GuiMainWindow::pointer()->setProgress(progress);
      }
    }    

    // write image data for testing and reporting purposes
    EG_VTKSP(vtkXMLImageDataWriter, vti);
    QString file_name = GuiMainWindow::pointer()->getCwd() + "/g.vti";
    vti->SetFileName(qPrintable(file_name));
    vti->SetDataModeToBinary();
    vti->SetInputData(gdata);
    vti->Write();

    gdata->GetPointData()->SetActiveScalars("g");
    EG_VTKSP(vtkContourFilter, contour);
    contour->SetInputData(gdata);
    contour->SetNumberOfContours(1);
    double g_level = 0.5;
    if (m_PreservationType == 1) {
      g_level = 0.5;
    }
    if (m_PreservationType == 2) {
      g_level = 0.5;
    }
    contour->SetValue(0, g_level);
    EG_VTKSP(vtkTriangleFilter, tri);
    tri->SetInputConnection(contour->GetOutputPort());
    EG_VTKSP(vtkDecimatePro, decimate);
    decimate->PreserveTopologyOn();
    decimate->SetTargetReduction(m_TargetReduction);
    decimate->SetFeatureAngle(GeometryTools::deg2rad(45));
    decimate->SetInputConnection(tri->GetOutputPort());
    decimate->Update();

    allocateGrid(m_Grid, decimate->GetOutput()->GetNumberOfPolys(), decimate->GetOutput()->GetNumberOfPoints());
    for (vtkIdType id_node = 0; id_node < m_Grid->GetNumberOfPoints(); ++id_node) {
      vec3_t x;
      decimate->GetOutput()->GetPoint(id_node, x.data());
      x[0] = m_X1[0] + x[0]*m_Dx;
      x[1] = m_X1[1] + x[1]*m_Dx;
      x[2] = m_X1[2] + x[2]*m_Dx;
      m_Grid->GetPoints()->SetPoint(id_node, x.data());
    }
    EG_VTKDCC(vtkIntArray, cell_code, m_Grid, "cell_code");
    for (vtkIdType id_cell = 0; id_cell < decimate->GetOutput()->GetNumberOfPolys(); ++id_cell) {
      EG_GET_CELL(id_cell, decimate->GetOutput());
      vtkIdType id_new_cell = m_Grid->InsertNextCell(type_cell, num_pts, pts);
      cell_code->SetValue(id_new_cell, 1);
    }
  }
}
Exemple #13
0
void BlenderReader::operate()
{
  try {
    QFileInfo file_info(GuiMainWindow::pointer()->getFilename());
    readInputFileName(file_info.completeBaseName() + ".begc", false);
    if (isValid()) {
      // read raw data from exported file
      QFile file(getFileName());
      file.open(QIODevice::ReadOnly | QIODevice::Text);
      QTextStream f(&file);
      QList<vec3_t> rnodes;
      QList<QVector<int> > rfaces;
      int num_parts;
      f >> num_parts;
      QVector<QString> part_name(num_parts);
      for (int i_part = 0; i_part < num_parts; ++i_part) {
        f >> part_name[i_part];
      }
      QVector<QString> sorted_part_name = part_name;
      qSort(sorted_part_name);
      QVector<int> part_bc(part_name.size());
      for (int i_part = 0; i_part < num_parts; ++i_part) {
        part_bc[i_part] = sorted_part_name.indexOf(part_name[i_part]) + 1;
      }
      for (int i_part = 0; i_part < num_parts; ++i_part) {
        int num_nodes, num_faces;
        f >> num_nodes >> num_faces;
        for (int i = 0; i < num_nodes; ++i) {
          vec3_t x;
          f >> x[0] >> x[1] >> x[2];
          rnodes.push_back(x);
        }
        for (int i = 0; i < num_faces; ++i) {
          int N;
          f >> N;
          QVector<int> face(N+1);
          face[0] = i_part;
          for (int j = 0; j < N; ++j) {
            f >> face[j+1];
          }
          rfaces.push_back(face);
        }
      }
      QVector<vec3_t> nodes(rnodes.size());
      qCopy(rnodes.begin(), rnodes.end(), nodes.begin());
      QVector<QVector<int> > faces(rfaces.size());
      qCopy(rfaces.begin(), rfaces.end(), faces.begin());

      // find smallest edge length
      double L = 1e99;
      foreach (QVector<int> face, faces) {
        for (int i = 1; i < face.size(); ++i) {
          int n1 = face[i];
          int n2 = face[1];
          if (i < face.size() - 1) {
            n2 = face[i+1];
          }
          double l = (nodes[n1] - nodes[n2]).abs();
          L = min(l, L);
        }
      }

      cout << "smallest edge length is " << L << endl;

      // delete duplicate nodes
      PointFinder finder;
      finder.setPoints(nodes);
      QList<vec3_t> non_dup;
      QVector<int> o2n(nodes.size());
      int num_non_dup = 0;
      for (int i = 0; i < nodes.size(); ++i) {
        o2n[i] = num_non_dup;
        bool dup = false;
        QVector<int> close_points;
        finder.getClosePoints(nodes[i], close_points);
        foreach (int j, close_points) {
          if (i > j) {
            double l = (nodes[i] - nodes[j]).abs();
            if (l < m_RelativeTolerance*L || l == 0) {
              o2n[i] = o2n[j];
              dup = true;
              break;
            }
          }
        }
        if (!dup) {
          non_dup.push_back(nodes[i]);
          ++num_non_dup;
        }
      }

      EG_VTKSP(vtkUnstructuredGrid, new_grid);

      allocateGrid(new_grid, faces.size(), non_dup.size());
      EG_VTKDCC(vtkIntArray, cell_code, new_grid, "cell_code");
      EG_VTKDCC(vtkIntArray, orgdir, new_grid, "cell_orgdir");
      EG_VTKDCC(vtkIntArray, voldir, new_grid, "cell_voldir");
      EG_VTKDCC(vtkIntArray, curdir, new_grid, "cell_curdir");
      vtkIdType id_node = 0;
      foreach (vec3_t x, non_dup) {
        new_grid->GetPoints()->SetPoint(id_node, x.data());
        ++id_node;
      }

      foreach (QVector<int> face, faces) {
        if (face.size() == 4) {
          vtkIdType pts[3];
          pts[0] = o2n[face[1]];
          pts[1] = o2n[face[2]];
          pts[2] = o2n[face[3]];
          vtkIdType id_cell = new_grid->InsertNextCell(VTK_TRIANGLE, 3, pts);
          cell_code->SetValue(id_cell, part_bc[face[0]]);
          orgdir->SetValue(id_cell, 0);
          voldir->SetValue(id_cell, 0);
          curdir->SetValue(id_cell, 0);
        }
        if (face.size() == 5) {
          vtkIdType pts[4];
          pts[0] = o2n[face[1]];
          pts[1] = o2n[face[2]];
          pts[2] = o2n[face[3]];
          pts[3] = o2n[face[4]];
          vtkIdType id_cell = new_grid->InsertNextCell(VTK_QUAD, 4, pts);
          cell_code->SetValue(id_cell, part_bc[face[0]]);
          orgdir->SetValue(id_cell, 0);
          voldir->SetValue(id_cell, 0);
          curdir->SetValue(id_cell, 0);
        }
      }

      if (m_Append) {
        EG_BUG;
        MeshPartition new_part(new_grid);
        new_part.setAllCells();
        m_Part.addPartition(new_part);
      } else {
        makeCopy(new_grid, m_Grid);
      }

      UpdateNodeIndex(m_Grid);
      UpdateCellIndex(m_Grid);

      // check and set the boundary names if required
      int update_required = true;
      QSet<int> old_bcs = GuiMainWindow::pointer()->getAllBoundaryCodes();
      if (old_bcs.size() == part_name.size()) {
        QSet<QString> old_names;
        foreach (int bc, old_bcs) {
          old_names.insert(GuiMainWindow::pointer()->getBC(bc).getName());
        }
        QSet<QString> new_names;
        foreach (QString name, part_name) {
          new_names.insert(name);
        }
void GuiSetBoundaryCode::operate()
{
  m_ButtonGroup->checkedId();
  cout<<"buttongroup->checkedId()="<<m_ButtonGroup->checkedId()<<endl;

  //save settings
  QSettings local_qset("enGits","enGrid_GuisetBoundaryCode");
  local_qset.setValue("FeatureAngle", m_Ui.doubleSpinBoxFeatureAngle->value());
  local_qset.setValue("BoundaryCode", m_Ui.spinBoxBoundaryCode->value());
  local_qset.setValue("PickMethod", m_ButtonGroup->checkedId());
  
  SetBoundaryCode set_bc;
  set_bc.setGrid(m_Grid);
  set_bc.setAllSurfaceCells();
  if (m_RadioButtonAuto->isChecked()) {
    QSet <int> display_bcs;
    GuiMainWindow::pointer()->getDisplayBoundaryCodes(display_bcs);
    int bc = m_Ui.spinBoxBoundaryCode->value();
    EG_VTKDCC(vtkIntArray, cell_code, m_Grid, "cell_code");
    for (vtkIdType id_cell = 0; id_cell < m_Grid->GetNumberOfCells(); ++id_cell) {
      bc = max(bc, cell_code->GetValue(id_cell));
      if (display_bcs.contains(cell_code->GetValue(id_cell))) {
        cell_code->SetValue(id_cell, 9999);
      }
    }
    bool done = false;
    do {
      vtkIdType id_start = -1;
      for (vtkIdType id_cell = 0; id_cell < m_Grid->GetNumberOfCells(); ++id_cell) {
        if (cell_code->GetValue(id_cell) == 9999) {
          id_start = id_cell;
          break;
        }
      }
      if (id_start == -1) {
        done = true;
      } else {
        set_bc.setFeatureAngle(m_Ui.doubleSpinBoxFeatureAngle->value());
        set_bc.setNewBC(bc);
        set_bc.setProcessAll(false);
        set_bc.setSelectAllVisible(false);
        set_bc.setOnlyPickedCell(false);
        set_bc.setOnlyPickedCellAndNeighbours(false);
        set_bc.setStart(id_start);
        QString bc_name = GuiMainWindow::pointer()->getBC(bc).getName();
        if (bc_name == "unknown") {
          bc_name.setNum(bc);
          bc_name = "wall_" + bc_name.rightJustified(3, '0');
          BoundaryCondition sym_bc(bc_name, "wall", bc);
          GuiMainWindow::pointer()->setBC(bc, sym_bc);
        }
        set_bc();
        ++bc;
      }
    } while (!done);
  } else {
    if (0 <= mainWindow()->getPickedCell() && mainWindow()->getPickedCell() < GuiMainWindow::pointer()->getGrid()->GetNumberOfCells() ) {
      set_bc.setFeatureAngle(m_Ui.doubleSpinBoxFeatureAngle->value());
      set_bc.setNewBC(m_Ui.spinBoxBoundaryCode->value());

      set_bc.setProcessAll(m_ButtonGroup->button(1)->isChecked());
      set_bc.setSelectAllVisible(m_ButtonGroup->button(2)->isChecked());
      set_bc.setOnlyPickedCell(m_ButtonGroup->button(3)->isChecked());
      set_bc.setOnlyPickedCellAndNeighbours(m_ButtonGroup->button(4)->isChecked());

      cout << "GuiMainWindow::getPickedCell()=" << mainWindow()->getPickedCell() << endl;
      set_bc.setStart(mainWindow()->getPickedCell());
      set_bc();
    } else {
      EG_ERR_RETURN("Please select a cell first.");
    }
  }
}
void MultiSolidAsciiStlReader::operate()
{
  QFileInfo file_info(GuiMainWindow::pointer()->getFilename());
  readInputFileName(file_info.completeBaseName() + ".stl");
  if (isValid()) {
    double tol = QInputDialog::getText(NULL, "enter STL tolerance", "tolerance", QLineEdit::Normal, "1e-10").toDouble();
    QList<QString> buffer;
    QList<QString> bc_name;
    {
      QFile file(getFileName());
      if (!file.open(QFile::ReadOnly)) {
        EG_ERR_RETURN("unable to open file");
      }
      QTextStream f(&file);
      QString buf = "";
      QString name = "unknown";
      while (!f.atEnd()) {
        QString line = f.readLine();
        buf += line + "\n"; // endline??
        if (line.left(8) == "endsolid") {
          buffer.append(buf);
          buf = "";
          bc_name.append(name);
        } else if (line.left(5) == "solid") {
          name = line.right(line.size() - 6);
        }
      }
    }
    bool first = true;
    int last_bc = 1;
    foreach (QString buf, buffer) {
      QString file_name = getFileName() + ".tmp";
      {
        QFile file(file_name);
        if (!file.open(QFile::WriteOnly)) {
          EG_ERR_RETURN("unable to open file\"" + file_name + "\" for writing");
        }
        QTextStream f(&file);
        f << buf << endl;
      }
      StlReader stl;
      stl.setTolerance(tol);
      stl.setFileName(file_name);
      EG_VTKSP(vtkUnstructuredGrid, grid);
      stl.setGrid(grid);
      stl.setMaximalCleaningIterations(3);
      stl();

      // @todo set boundary names
      EG_VTKDCC(vtkIntArray, bc, grid, "cell_code");
      for (vtkIdType id_cell = 0; id_cell < grid->GetNumberOfCells(); ++id_cell) {
        bc->SetValue(id_cell, last_bc);
      }
      ++last_bc;

      if (first) {
        first = false;
        makeCopy(grid, m_Grid);
      } else {
        MeshPartition part1(m_Grid, true);
        MeshPartition part2(grid, true);
        part1.addPartition(part2);
      }
    }

    last_bc = 1;
    GuiMainWindow::pointer()->resetXmlDoc();
    GuiMainWindow::pointer()->clearBCs();
    foreach (QString name, bc_name) {
      GuiMainWindow::pointer()->addBC(last_bc, BoundaryCondition(name, "patch"));
      ++last_bc;
    }
Exemple #16
0
void StlReader::operate()
{
  QFileInfo file_info(GuiMainWindow::pointer()->getFilename());
  QString file_name;
  if (m_FileNameSet) {
    file_name = m_FileName;
  } else {
    readInputFileName(file_info.completeBaseName() + ".stl");
    if (isValid()) {
      file_name = getFileName();
    } else {
      return;
    }
  }

  EG_VTKSP(vtkSTLReader, stl);
  stl->MergingOn();
  stl->SetFileName(qPrintable(file_name));
  stl->Update();
  EG_VTKSP(vtkPolyData, poly);
  poly->DeepCopy(stl->GetOutput());
  poly->BuildCells();
  double L = 1e99;
  for (vtkIdType cellId = 0; cellId < poly->GetNumberOfCells(); ++cellId) {
    vtkIdType *pts, Npts;
    poly->GetCellPoints(cellId, Npts, pts);
    for (int i = 0; i < Npts; ++i) {
      vec3_t x1, x2;
      poly->GetPoints()->GetPoint(pts[i], x1.data());
      if (i == Npts - 1) {
        poly->GetPoints()->GetPoint(pts[0], x2.data());
      } else {
        poly->GetPoints()->GetPoint(pts[i+1], x2.data());
      }
      L = min(L, (x1-x2).abs());
    }
  }
  if (m_Tolerance < 0) {
    m_Tolerance = QInputDialog::getText(NULL, "enter STL tolerance", "tolerance", QLineEdit::Normal, "1e-10").toDouble();
  }
  cout << "cleaning STL geometry:" << endl;
  EG_VTKSP(vtkCleanPolyData, poly_clean);
  EG_VTKSP(vtkFeatureEdges, topo_check);
  double bounds[6];
  poly->GetBounds(bounds);
  poly_clean->ToleranceIsAbsoluteOn();
  poly_clean->ConvertLinesToPointsOn();
  poly_clean->ConvertPolysToLinesOn();
  poly_clean->SetInputData(poly);
  topo_check->SetInputConnection(poly_clean->GetOutputPort());
  topo_check->BoundaryEdgesOn();
  topo_check->ManifoldEdgesOff();
  topo_check->FeatureEdgesOff();
  topo_check->NonManifoldEdgesOn();
  bool check_passed;
  int count = 0;
  do {
    ++count;
    cout << "  tolerance = " << m_Tolerance << endl;
    poly_clean->SetAbsoluteTolerance(m_Tolerance);
    topo_check->Update();
    m_Tolerance *= 1.5;
    check_passed = topo_check->GetOutput()->GetNumberOfPoints() == 0;
  } while (m_Tolerance < 1 && !check_passed && count < m_MaxNumCleanIter);
  if (check_passed) {
    cout << "The STL geometry seems to be clean." << endl;
  } else {
    cout << "The STL geometry could not be cleaned." << endl;
  }
  // with a tolerance of " << 0.5*L << endl;
  EG_VTKSP(vtkEgPolyDataToUnstructuredGridFilter, poly2ugrid);
  poly2ugrid->SetInputConnection(poly_clean->GetOutputPort());
  poly2ugrid->Update();

  allocateGrid(m_Grid, poly2ugrid->GetOutput()->GetNumberOfCells(), poly2ugrid->GetOutput()->GetNumberOfPoints());
  for (vtkIdType id_node = 0; id_node < poly2ugrid->GetOutput()->GetNumberOfPoints(); ++id_node) {
    vec3_t x;
    poly2ugrid->GetOutput()->GetPoints()->GetPoint(id_node, x.data());
    m_Grid->GetPoints()->SetPoint(id_node, x.data());
  }
  for (vtkIdType id_cell = 0; id_cell < poly2ugrid->GetOutput()->GetNumberOfCells(); ++id_cell) {
    vtkIdType N_pts, *pts;
    vtkIdType type_cell = poly2ugrid->GetOutput()->GetCellType(id_cell);
    poly2ugrid->GetOutput()->GetCellPoints(id_cell, N_pts, pts);
    m_Grid->InsertNextCell(type_cell, N_pts, pts);
  }

  EG_VTKDCC(vtkIntArray, bc, m_Grid, "cell_code");
  EG_VTKDCC(vtkIntArray, orgdir, m_Grid, "cell_orgdir");
  EG_VTKDCC(vtkIntArray, voldir, m_Grid, "cell_voldir");
  EG_VTKDCC(vtkIntArray, curdir, m_Grid, "cell_curdir");
  for (vtkIdType id_cell = 0; id_cell < m_Grid->GetNumberOfCells(); ++id_cell) {
    bc->SetValue(id_cell, 1);
    orgdir->SetValue(id_cell, 0);
    voldir->SetValue(id_cell, 0);
    curdir->SetValue(id_cell, 0);
  }
  if (check_passed) {
    CorrectSurfaceOrientation corr_surf;
    corr_surf.setGrid(m_Grid);
    corr_surf();
    FixCadGeometry cad_fix;
    cad_fix.setGrid(m_Grid);
    cad_fix();
  }
}
Exemple #17
0
void TauWriter::operate()
{
  try {
    QFileInfo file_info(GuiMainWindow::pointer()->getFilename());
    readOutputFileName(file_info.completeBaseName() + ".grid");
    if (isValid()) {
      QString file_name = getFileName();
      NcFile *nc_file = new NcFile(file_name.toAscii(), NcFile::Replace);
      if (!nc_file->is_valid()) {
        EG_ERR_RETURN("unable to open NetCFD file for writing");
      }

      // point coordinates
      size_t Np = m_Grid->GetNumberOfPoints();
      vector<double> x(Np),y(Np),z(Np);
      for (vtkIdType id_node = 0; id_node < m_Grid->GetNumberOfPoints(); ++id_node) {
        vec3_t xv;
        m_Grid->GetPoint(id_node, xv.data());
        x[id_node] = xv[0];
        y[id_node] = xv[1];
        z[id_node] = xv[2];
      }
      NcDim *no_of_points = nc_file->add_dim("no_of_points", Np);
      NcVar *points_xc = nc_file->add_var("points_xc", ncDouble, no_of_points);
      NcVar *points_yc = nc_file->add_var("points_yc", ncDouble, no_of_points);
      NcVar *points_zc = nc_file->add_var("points_zc", ncDouble, no_of_points);
      points_xc->put(&x[0],Np);
      points_yc->put(&y[0],Np);
      points_zc->put(&z[0],Np);

      // boundary faces
      size_t Nbe   = 0;;
      size_t Nquad = 0;
      size_t Ntri  = 0;
      for (vtkIdType id_cell = 0; id_cell < m_Grid->GetNumberOfCells(); ++id_cell) {
        if (isSurface(id_cell, m_Grid)) {
          ++Nbe;
          if (m_Grid->GetCellType(id_cell) == VTK_TRIANGLE) {
            ++Ntri;
          } else if (m_Grid->GetCellType(id_cell) == VTK_QUAD) {
            ++Nquad;
          } else {
            EG_ERR_RETURN("unsupported boundary element type encountered");
          }
        }
      }
      NcDim *no_of_surfaceelements = nc_file->add_dim("no_of_surfaceelements", Nbe);
      NcVar *boundarymarker_of_surfaces = nc_file->add_var("boundarymarker_of_surfaces", ncInt, no_of_surfaceelements);
      vector<int> bm(Nbe,0), tri(3*Ntri), quad(4*Nquad);
      int i_bm = 0;
      QSet<int> bc_set = getAllBoundaryCodes(m_Grid);
      QVector<int> bcs(bc_set.size());
      qCopy(bc_set.begin(), bc_set.end(), bcs.begin());
      EG_VTKDCC(vtkIntArray, cell_code, m_Grid, "cell_code");
      if (Ntri) {
        NcDim *no_of_surfacetriangles = nc_file->add_dim("no_of_surfacetriangles", Ntri);
        NcDim *points_per_surfacetriangle = nc_file->add_dim("points_per_surfacetriangle", 3);
        NcVar *points_of_surfacetriangles = nc_file->add_var("points_of_surfacetriangles", ncInt, no_of_surfacetriangles, points_per_surfacetriangle);
        int i_tri = 0;
        for (vtkIdType id_cell = 0; id_cell < m_Grid->GetNumberOfCells(); ++id_cell) {
          if (isSurface(id_cell, m_Grid)) {
            if (m_Grid->GetCellType(id_cell) == VTK_TRIANGLE) {
              for (int i_bc = 0; i_bc < bcs.size(); ++i_bc) {
                if (bcs[i_bc] == cell_code->GetValue(id_cell)) {
                  bm[i_bm] = i_bc+1;
                  break;
                }
              }
              vtkIdType N_pts, *pts;
              m_Grid->GetCellPoints(id_cell, N_pts, pts);
              tri[i_tri + 0] = pts[0];
              tri[i_tri + 1] = pts[1];
              tri[i_tri + 2] = pts[2];
              ++i_bm;
              i_tri += 3;
            }
          }
        }
        points_of_surfacetriangles->put(&tri[0],Ntri,3);
      }
      if (Nquad) {
        NcDim *no_of_surfacequadrilaterals     = nc_file->add_dim("no_of_surfacequadrilaterals",Nquad);
        NcDim *points_per_surfacequadrilateral = nc_file->add_dim("points_per_surfacequadrilateral",4);
        NcVar *points_of_surfacequadrilaterals = nc_file->add_var("points_of_surfacequadrilaterals",ncInt, no_of_surfacequadrilaterals, points_per_surfacequadrilateral);
        int i_quad = 0;
        for (vtkIdType id_cell = 0; id_cell < m_Grid->GetNumberOfCells(); ++id_cell) {
          if (isSurface(id_cell, m_Grid)) {
            if (m_Grid->GetCellType(id_cell) == VTK_QUAD) {
              for (int i_bc = 0; i_bc < bcs.size(); ++i_bc) {
                if (bcs[i_bc] == cell_code->GetValue(id_cell)) {
                  bm[i_bm] = i_bc+1;
                  break;
                }
              }
              vtkIdType N_pts, *pts;
              m_Grid->GetCellPoints(id_cell, N_pts, pts);
              quad[i_quad + 0] = pts[0];
              quad[i_quad + 1] = pts[1];
              quad[i_quad + 2] = pts[2];
              quad[i_quad + 3] = pts[3];
              ++i_bm;
              i_quad += 4;
            }
          }
        }
        points_of_surfacequadrilaterals->put(&quad[0],Nquad,4);
      }
      boundarymarker_of_surfaces->put(&bm[0],Nbe);
      NcDim *no_of_markers = nc_file->add_dim("no_of_markers", bcs.size());


      int Ntet = 0;
      int Npri = 0;
      int Nhex = 0;
      int Npyr = 0;
      for (vtkIdType id_cell = 0; id_cell < m_Grid->GetNumberOfCells(); ++id_cell) {
        if (m_Grid->GetCellType(id_cell) == VTK_TETRA)      ++Ntet;
        if (m_Grid->GetCellType(id_cell) == VTK_PYRAMID)    ++Npyr;
        if (m_Grid->GetCellType(id_cell) == VTK_WEDGE)      ++Npri;
        if (m_Grid->GetCellType(id_cell) == VTK_HEXAHEDRON) ++Nhex;
      }

      vector<int> tet(Ntet*4),pyr(Npyr*5),pri(Npri*6),hex(Nhex*8);
      int i_tet = 0;
      int i_pyr = 0;
      int i_pri = 0;
      int i_hex = 0;
      if (Ntet) {
        NcDim *no_of_tetraeders = nc_file->add_dim("no_of_tetraeders",Ntet);
        NcDim *points_per_tetraeder = nc_file->add_dim("points_per_tetraeder",4);
        NcVar *points_of_tetraeders = nc_file->add_var("points_of_tetraeders",ncInt, no_of_tetraeders, points_per_tetraeder);
        for (vtkIdType id_cell = 0; id_cell < m_Grid->GetNumberOfCells(); ++id_cell) {
          if (m_Grid->GetCellType(id_cell) == VTK_TETRA) {
            vtkIdType *pts, N_pts;
            m_Grid->GetCellPoints(id_cell, N_pts, pts);
            tet[i_tet + 0] = pts[0];
            tet[i_tet + 1] = pts[1];
            tet[i_tet + 2] = pts[2];
            tet[i_tet + 3] = pts[3];
            i_tet += 4;
          }
        }
        points_of_tetraeders->put(&tet[0],Ntet,4);
      }
      if (Npyr) {
        NcDim *no_of_pyramids = nc_file->add_dim("no_of_pyramids",Npyr);
        NcDim *points_per_pyramid = nc_file->add_dim("points_per_pyramid",5);
        NcVar *points_of_pyramids = nc_file->add_var("points_of_pyramids",ncInt, no_of_pyramids, points_per_pyramid);
        for (vtkIdType id_cell = 0; id_cell < m_Grid->GetNumberOfCells(); ++id_cell) {
          if (m_Grid->GetCellType(id_cell) == VTK_PYRAMID) {
            vtkIdType *pts, N_pts;
            m_Grid->GetCellPoints(id_cell, N_pts, pts);
            pyr[i_pyr + 0] = pts[0];
            pyr[i_pyr + 1] = pts[1];
            pyr[i_pyr + 2] = pts[2];
            pyr[i_pyr + 3] = pts[3];
            pyr[i_pyr + 4] = pts[4];
            i_pyr += 5;
          }
        }
        points_of_pyramids->put(&pyr[0],Npyr,5);
      }
      if (Npri) {
        NcDim *no_of_prisms = nc_file->add_dim("no_of_prisms",Npri);
        NcDim *points_per_prism = nc_file->add_dim("points_per_prism",6);
        NcVar *points_of_prisms = nc_file->add_var("points_of_prisms",ncInt, no_of_prisms,  points_per_prism);
        for (vtkIdType id_cell = 0; id_cell < m_Grid->GetNumberOfCells(); ++id_cell) {
          if (m_Grid->GetCellType(id_cell) == VTK_WEDGE) {
            vtkIdType *pts, N_pts;
            m_Grid->GetCellPoints(id_cell, N_pts, pts);
            pri[i_pri + 0] = pts[0];
            pri[i_pri + 1] = pts[2];
            pri[i_pri + 2] = pts[1];
            pri[i_pri + 3] = pts[3];
            pri[i_pri + 4] = pts[5];
            pri[i_pri + 5] = pts[4];
            i_pri += 6;
          }
        }
        points_of_prisms->put(&pri[0],Npri,6);
      }
      if (Nhex) {
        NcDim *no_of_hexaeders = nc_file->add_dim("no_of_hexaeders",Nhex);
        NcDim *points_per_hexaeder = nc_file->add_dim("points_per_hexaeder",8);
        NcVar *points_of_hexaeders = nc_file->add_var("points_of_hexaeders",ncInt, no_of_hexaeders, points_per_hexaeder);
        for (vtkIdType id_cell = 0; id_cell < m_Grid->GetNumberOfCells(); ++id_cell) {
          if (m_Grid->GetCellType(id_cell) == VTK_HEXAHEDRON) {
            vtkIdType *pts, N_pts;
            m_Grid->GetCellPoints(id_cell, N_pts, pts);
            hex[i_hex + 0] = pts[4];
            hex[i_hex + 1] = pts[7];
            hex[i_hex + 2] = pts[6];
            hex[i_hex + 3] = pts[5];
            hex[i_hex + 4] = pts[0];
            hex[i_hex + 5] = pts[3];
            hex[i_hex + 6] = pts[2];
            hex[i_hex + 7] = pts[1];
            i_hex += 8;
          }
        }
        points_of_hexaeders->put(&hex[0],Nhex,8);
      }

      delete nc_file;
    }
  } catch (Error err) {
    err.display();
  }
}
void GuiNormalExtrusion::operate()
{
  QSet<int> bcs;
  getSelectedItems(m_Ui.listWidget, bcs);
  EG_VTKDCC(vtkIntArray, cell_code, m_Grid, "cell_code");
  QSet<int> volume_codes;
  EG_FORALL_CELLS(id_cell, m_Grid) {
    if (isSurface(id_cell, m_Grid)) {
      if (bcs.contains(cell_code->GetValue(id_cell))) {
        vtkIdType id_volume_cell = m_Part.getVolumeCell(id_cell);
        if (id_volume_cell != -1) {
          volume_codes.insert(cell_code->GetValue(id_volume_cell));
        }
      }
    }
  }

  EG_VTKSP(vtkEgNormalExtrusion, extr);
  QVector<double> y;
  
  if (m_Ui.radioButtonSimple->isChecked()) {
    y.resize(m_Ui.lineEditSimpleNumLayers->text().toInt() + 1);
    double h = m_Ui.lineEditSimpleHeight->text().toDouble();
    double f = m_Ui.lineEditSimpleIncrease->text().toDouble();
    y[0] = 0.0;
    for (int i = 1; i < y.size(); ++i) {
      y[i] = y[i-1] + h;
      h *= f;
    }
  } else if (m_Ui.radioButtonFixedHeights->isChecked()) {
    y.resize(m_Ui.lineEditFixedHeightsNumLayers->text().toInt() + 1);
    QVector<double> x(y.size());
    for (int i = 0; i < x.size(); ++i) {
      x[i] = i*1.0/(x.size() - 1);
    }
    mat3_t A;
    clinit(A[0]) = pow(x[1],5.0), pow(x[1],3.0), x[1];
    clinit(A[1]) = pow(x[x.size() - 2],5.0), pow(x[x.size() - 2],3.0), x[x.size() - 2];
    clinit(A[2]) = pow(x[x.size() - 1],5.0), pow(x[x.size() - 1],3.0), x[x.size() - 1];
    vec3_t h;
    h[0] = m_Ui.lineEditFixedHeightsHeightFirst->text().toDouble();
    h[2] = m_Ui.lineEditFixedHeightsTotalHeight->text().toDouble();
    h[1] = h[2] - m_Ui.lineEditFixedHeightsHeightLast->text().toDouble();
    mat3_t AI = A.inverse();
    vec3_t coeff = AI*h;
    for (int i = 0; i < y.size(); ++i) {
      y[i] = coeff[0]*pow(x[i],5.0) + coeff[1]*pow(x[i],3.0) + coeff[2]*x[i];
      if (i > 0) {
        if (y[i] < y[i-1]) {
          EG_ERR_RETURN("unable to compute layer heights");
        }
      }
    }
  }
  extr->SetLayers(y);
  
  if (m_Ui.radioButtonFixed->isChecked()) {
    extr->SetNormal(vec3_t(m_Ui.lineEditFixedNX->text().toDouble(),
                           m_Ui.lineEditFixedNY->text().toDouble(),
                           m_Ui.lineEditFixedNZ->text().toDouble()));
    double min_dist = m_Ui.lineEditFixedDist->text().toDouble();
    if (min_dist <= 0) {
      extr->SetFixed();
    } else {
      extr->SetPlanar();
      extr->SetMinDist(min_dist);
    }
  }
  if (m_Ui.radioButtonCylinder->isChecked()) {
    extr->SetCylindrical();
    extr->SetOrigin(vec3_t(m_Ui.lineEditCylinderX0->text().toDouble(),
                           m_Ui.lineEditCylinderY0->text().toDouble(),
                           m_Ui.lineEditCylinderZ0->text().toDouble()));
    extr->SetAxis(vec3_t(m_Ui.lineEditCylinderNX->text().toDouble(),
                         m_Ui.lineEditCylinderNY->text().toDouble(),
                         m_Ui.lineEditCylinderNZ->text().toDouble()));
  }
  if (m_Ui.radioButtonRotation->isChecked()) {
    extr->SetRotation();
    extr->SetOrigin(vec3_t(m_Ui.lineEditCylinderX0->text().toDouble(),
                           m_Ui.lineEditCylinderY0->text().toDouble(),
                           m_Ui.lineEditCylinderZ0->text().toDouble()));
    extr->SetAxis(vec3_t(m_Ui.lineEditCylinderNX->text().toDouble(),
                         m_Ui.lineEditCylinderNY->text().toDouble(),
                         m_Ui.lineEditCylinderNZ->text().toDouble()));
  }

  if (m_Ui.radioButtonNoRestrict->isChecked()) {
    extr->SetRestrictNone();
  }
  if (m_Ui.radioButtonXY->isChecked()) {
    extr->SetRestrictXY();
  }
  if (m_Ui.radioButtonXZ->isChecked()) {
    extr->SetRestrictXZ();
  }
  if (m_Ui.radioButtonYZ->isChecked()) {
    extr->SetRestrictYZ();
  }

  if (m_Ui.checkBoxNewVolume->isChecked()) {
    extr->SetRemoveInternalFacesOff();
  }

  QList<VolumeDefinition> vols = GuiMainWindow::pointer()->getAllVols();
  int vc = 1;
  foreach (VolumeDefinition vol, vols) {
    vc = max(vc, vol.getVC());
  }
Exemple #19
0
void BrlcadReader::processStlFile(QString file_name, bool append_to_list)
{
  vtkSTLReader *stl = vtkSTLReader::New();
  stl->MergingOn();
  stl->SetFileName(file_name.toAscii().data());
  stl->Update();
  EG_VTKSP(vtkPolyData, poly);
  poly->DeepCopy(stl->GetOutput());
  poly->BuildCells();
  double L = 1e99;
  for (vtkIdType cellId = 0; cellId < poly->GetNumberOfCells(); ++cellId) {
    vtkIdType *pts, Npts;
    poly->GetCellPoints(cellId, Npts, pts);
    for (int i = 0; i < Npts; ++i) {
      vec3_t x1, x2;
      poly->GetPoints()->GetPoint(pts[i], x1.data());
      if (i == Npts - 1) {
        poly->GetPoints()->GetPoint(pts[0], x2.data());
      } else {
        poly->GetPoints()->GetPoint(pts[i+1], x2.data());
      }
      L = min(L, (x1-x2).abs());
    }
  }
  EG_VTKSP(vtkEgPolyDataToUnstructuredGridFilter, poly2ugrid);
  poly2ugrid->SetInput(poly);
  poly2ugrid->Update();

  EG_VTKSP(vtkUnstructuredGrid, grid);
  allocateGrid(grid, poly2ugrid->GetOutput()->GetNumberOfCells(), poly2ugrid->GetOutput()->GetNumberOfPoints());
  for (vtkIdType id_node = 0; id_node < poly2ugrid->GetOutput()->GetNumberOfPoints(); ++id_node) {
    vec3_t x;
    poly2ugrid->GetOutput()->GetPoints()->GetPoint(id_node, x.data());
    grid->GetPoints()->SetPoint(id_node, x.data());
  }
  for (vtkIdType id_cell = 0; id_cell < poly2ugrid->GetOutput()->GetNumberOfCells(); ++id_cell) {
    vtkIdType N_pts, *pts;
    vtkIdType type_cell = poly2ugrid->GetOutput()->GetCellType(id_cell);
    poly2ugrid->GetOutput()->GetCellPoints(id_cell, N_pts, pts);
    grid->InsertNextCell(type_cell, N_pts, pts);
  }

  EG_VTKDCC(vtkIntArray, cell_code, grid, "cell_code");
  EG_VTKDCC(vtkIntArray, orgdir,    grid, "cell_orgdir");
  EG_VTKDCC(vtkIntArray, voldir,    grid, "cell_voldir");
  EG_VTKDCC(vtkIntArray, curdir,    grid, "cell_curdir");
  for (vtkIdType id_cell = 0; id_cell < grid->GetNumberOfCells(); ++id_cell) {
    cell_code->SetValue(id_cell, 9999);
    orgdir->SetValue(id_cell, 0);
    voldir->SetValue(id_cell, 0);
    curdir->SetValue(id_cell, 0);
  }

  if (append_to_list) {
    SetBoundaryCode set_bc;
    set_bc.setGrid(grid);
    set_bc.setAllSurfaceCells();
    int bc_max = 1;
    bool done = false;
    do {
      vtkIdType id_start = -1;
      for (vtkIdType id_cell = 0; id_cell < grid->GetNumberOfCells(); ++id_cell) {
        if (cell_code->GetValue(id_cell) == 9999) {
          id_start = id_cell;
          break;
        }
      }
      if (id_start == -1) {
        done = true;
      } else {
        set_bc.setFeatureAngle(20.0);
        set_bc.setBC(bc_max);
        set_bc.setProcessAll(true);
        set_bc.setSelectAllVisible(false);
        set_bc.setOnlyPickedCell(false);
        set_bc.setOnlyPickedCellAndNeighbours(false);
        set_bc.setStart(id_start);
        set_bc();
        ++bc_max;
      }
    } while (!done);
    cout << "file: " << qPrintable(file_name) << endl;
    for (int bc = 1; bc < bc_max; ++bc) {
      QList<vtkIdType> cells;
      for (vtkIdType id_cell = 0; id_cell < grid->GetNumberOfCells(); ++id_cell) {
        if (cell_code->GetValue(id_cell) == bc) {
          cells.append(id_cell);
        }
      }
      vtkUnstructuredGrid *new_grid = vtkUnstructuredGrid::New();
      QString bc_txt;
      bc_txt.setNum(bc);
      if (bc_max >= 10) {
        bc_txt = bc_txt.rightJustified(2, '0');
      }
      QFileInfo file_info(file_name);
      m_BCNames[new_grid] = file_info.baseName() + "." + bc_txt;
      cout << "  " << qPrintable(file_info.baseName() + "." + bc_txt) << endl;
      makeCopy(grid, new_grid, cells);
      m_Grids.append(new_grid);
    }
  } else {
    makeCopy(grid, m_Grid);
  }
}
void SimpleFoamWriter::createFaces()
{
  l2g_t cells = getPartCells();

  m_LFaces.clear();
  EG_VTKDCC(vtkIntArray, cell_code,   m_Grid, "cell_code");
  m_BC = cell_code;
  m_Eg2Of.fill(-1,cells.size());
  int Nvol = 0;
  foreach(int i_cells, cells) {
    vtkIdType *pts;
    vtkIdType  Npts;
    m_Grid->GetCellPoints(cells[i_cells], Npts, pts);
    vtkIdType type_cell = m_Grid->GetCellType(cells[i_cells]);
    vtkIdType id_cell = cells[i_cells];
    
    // tetras
    //
    if (type_cell == VTK_TETRA) {
      m_Eg2Of[id_cell] = Nvol++;
      {      
        face_t F(3,id_cell,getNeigh(i_cells,0));
        F.node[0] = pts[2]; F.node[1] = pts[1]; F.node[2] = pts[0];
        addFace(F);
      }
      {
        face_t F(3,id_cell,getNeigh(i_cells,1));
        F.node[0] = pts[0]; F.node[1] = pts[1]; F.node[2] = pts[3];
        addFace(F);
      }
      {
        face_t F(3,id_cell,getNeigh(i_cells,2));
        F.node[0] = pts[0]; F.node[1] = pts[3]; F.node[2] = pts[2];
        addFace(F);
      }
      {
        face_t F(3,id_cell,getNeigh(i_cells,3));
        F.node[0] = pts[1]; F.node[1] = pts[2]; F.node[2] = pts[3];
        addFace(F);
      }
    }
    
    // prisms
    //
    if (type_cell == VTK_WEDGE) {
      m_Eg2Of[id_cell] = Nvol++;
      {
        face_t F(3,id_cell,getNeigh(i_cells,0));
        F.node[0] = pts[0]; F.node[1] = pts[1]; F.node[2] = pts[2];
        addFace(F);
      }
      {
        face_t F(3,id_cell,getNeigh(i_cells,1));
        F.node[0] = pts[3]; F.node[1] = pts[5]; F.node[2] = pts[4];
        addFace(F);
      }
      {
        face_t F(4,id_cell,getNeigh(i_cells,2));
        F.node[0] = pts[3]; F.node[1] = pts[4]; F.node[2] = pts[1]; F.node[3] = pts[0];
        addFace(F);
      }
      {
        face_t F(4,id_cell,getNeigh(i_cells,3));
        F.node[0] = pts[1]; F.node[1] = pts[4]; F.node[2] = pts[5]; F.node[3] = pts[2];
        addFace(F);
      }
      {
        face_t F(4,id_cell,getNeigh(i_cells,4));
        F.node[0] = pts[0]; F.node[1] = pts[2]; F.node[2] = pts[5]; F.node[3] = pts[3];
        addFace(F);
      }
    }
    
    // hexes
    //
    if (type_cell == VTK_HEXAHEDRON) {
      m_Eg2Of[id_cell] = Nvol++;
      {
        face_t F(4,id_cell,getNeigh(i_cells,0),0);
        F.node[0] = pts[3]; F.node[1] = pts[2]; F.node[2] = pts[1]; F.node[3] = pts[0];
        addFace(F);
      }
      {
        face_t F(4,id_cell,getNeigh(i_cells,1),0);
        F.node[0] = pts[4]; F.node[1] = pts[5]; F.node[2] = pts[6]; F.node[3] = pts[7];
        addFace(F);
      }
      {
        face_t F(4,id_cell,getNeigh(i_cells,2),0);
        F.node[0] = pts[0]; F.node[1] = pts[1]; F.node[2] = pts[5]; F.node[3] = pts[4];
        addFace(F);
      }
      {
        face_t F(4,id_cell,getNeigh(i_cells,3),0);
        F.node[0] = pts[3]; F.node[1] = pts[7]; F.node[2] = pts[6]; F.node[3] = pts[2];
        addFace(F);
      }
      {
        face_t F(4,id_cell,getNeigh(i_cells,4),0);
        F.node[0] = pts[0]; F.node[1] = pts[4]; F.node[2] = pts[7]; F.node[3] = pts[3];
        addFace(F);
      }
      {
        face_t F(4,id_cell,getNeigh(i_cells,5),0);
        F.node[0] = pts[1]; F.node[1] = pts[2]; F.node[2] = pts[6]; F.node[3] = pts[5];
        addFace(F);
      }
    }
  }